字符串的操作方法

capitalize() : 首字母大写

s1 = 'my heart will go on'
print(s1.capitalize()) # 首字母大写 # 执行结果:
# My heart will go on

capitalize

upper() : 全部大写

s1 = 'my heart will go on'
print(s1.upper()) # 全部转为大写 # 执行结果:
# MY HEART WILL GO ON

upper

lower() : 全部小写

s1 = 'my heart will go on'
print(s1.lower()) # 全部转为小写 # 执行结果:
# my heart will go on

lower

这里写一个使用大小写转换的实例,验证码的比较:

auth_code = 'KDwq'

user_code = input('请输入验证码(不区分大小写):')
if user_code.upper() == auth_code.upper(): # 将验证码和用户输入的字符统一转换为大写进行比较;
print('验证成功.')
else:
print('验证失败.')

swapcase() : 大小写反转

s1 = 'MY HEART will go ON'
print(s1.swapcase()) # 大小写反转;大写转换为小写,小写转换为大写 # 执行结果:
# my heart WILL GO on

swapcase

title(): 标题首字母大写

s1 = 'MY HEART will go ON'
print(s1.title()) # 标题首字母大写,用空格隔开的每个单词都当作一个标题处理
# # 执行结果:
# My Heart Will Go On

title

center(): 居中,默认填充空格

s1 = 'MY HEART will go ON'
print(s1.center(30, '#')) # 居中显示,第一个参数是位宽int类型,第二个参数是占位符,str类型; # 执行结果:
# #####MY HEART will go ON######

center

startswith(): 以什么开头,返回 bool 值

s1 = 'nice to meeting you'
print(s1.startswith('ni')) # 判断以什么字符串开始,返回bool类型,正确返回 True,错误返回 False # 执行结果:
# True print(s1.startswith('no')) # 执行结果:
# False

startswith

endswith(): 以什么结尾,返回 bool 值

s1 = 'nice to meeting you'
print(s1.endswith('you')) # 判断以什么字符串结尾,返回bool类型,正确返回 True,错误返回 False # 执行结果:
# True print(s1.endswith('yyy')) # 执行结果:
# False

endswith

find() 和 index() 都是通过元素找索引的方法,区别如下:

find()方法:元素存在打印索引,元素不存在返回 -1

index()方法:元素存在打印索引,元素不存在报错。错误信息:ValueError: substring not found

s1 = 'nice to meeting you'

print(s1.find('to'))

# 执行结果:
# print(s1.find('xxx')) # 执行结果:
# -1 print(s1.index('to')) # 执行结果:
# # print(s1.index('xxx')) # 执行结果:
# ValueError: substring not found

find 和 index

所以,在程序中应当使用 find() 来查找索引,尽量避免程序出现报错信息

strip(): 默认去除行首和行尾的空格;
    rstrip() 从左删
    lstrip() 从右删

s1 = '    nice to meeting you    '

print(s1.strip())   # 默认去除开头和结尾的空格部分;

# 执行结果:
# nice to meeting you s1 = '#######nice to meeting you#######'
print(s1.strip('#')) # 第一个参数为开头和结尾要去除的字符 # 执行结果:
# nice to meeting you

strip

count():统计字符串出现个数

s1 = 'adfasdfasdfasdfertadf'
print(s1.count('a')) # 统计字符串出现的次数 # 执行结果:
#

count

split(): 按照指定字符分隔成多个元素  str --> list

s1 = 'nice to meeting you'
print(s1.split()) # 默认将字符串通过空格分隔并组成列表类型 # 执行结果:
# ['nice', 'to', 'meeting', 'you']

split

format 有三种用法:

print('我叫{}, 跟我念一遍:{}, 年龄:{}, 爱好:{}'.format('clot', 'clot', 20, '摄影'))
print('我叫{0}, 跟我念一遍:{0}, 年龄:{1}, 爱好:{2}'.format('clot', 20, '摄影'))
print('我叫{name}, 跟我念一遍:{name}, 年龄:{age}, 爱好:{hobby}'.format(name='clot', age=20, hobby='摄影')) # 执行结果:
#
# 我叫clot, 跟我念一遍:clot, 年龄:20, 爱好:摄影
# 我叫clot, 跟我念一遍:clot, 年龄:20, 爱好:摄影
# 我叫clot, 跟我念一遍:clot, 年龄:20, 爱好:摄影

format

replace(): 字符串替换

s1 = 'nice to meeting you'
print(s1.replace('meeting', 'see')) # 在字符串中,用第一个参数替换掉第二个参数,默认是匹配上则全部替换 # 执行结果:
# nice to see you

replace

isdigit(): 判断字符串是否全部是数字组成

s1 = ''
print(s1.isdigit()) # 判断字符串是否全部由数字组成,小数点无法匹配上,返回bool类型; # 执行结果:
# True

isdigit

isalpha():判断字符串是否全是字母组成

s1 = 'asdfasdf'

print(s1.isalpha()) # 判断字符串是否全部由字母组成,返回bool类型;

# 执行结果:
# True

isalpha

isalnum():判断字符串是否由数字和字母组成

s1 = 'asdfa234asdf'

print(s1.isalnum()) # 判断字符串是否全部由字母或字母组成,返回bool类型;

# 执行结果:
# True

isalnum

作业题

1. 判断下列逻辑语句的 True、False

1) 1 > 1 or 3 > 4 or 4 > 5 and 2> 1 and 9 > 8 or 7 < 6
2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
3) 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2

2. 求出下列逻辑语句的值。

1) 8 or 3 and 4 or 2 and 0 or 9 and 7
2) 0 or 2 and 3 and 4 or 6 and 0 or 3
3) 5 and 9 or 10 and 2 or 3 and 5 or 4 or 5

3. 下列结果是什么?
1) 6 or 2 > 1
2) 3 or 2 > 1
3) 0 or 5 < 4
4) 5 < 4 or 3
5) 2 > 1 or 6
6) 3 and 2 > 1
7) 0 and 3 > 1
8) 2 > 1 and 3
9) 3 > 1 and 0
10) 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

4. 写代码:计算 1-2+3...+99 中除了88意外所有数的总和?

以下 F 表示: False, T 表示:True

1. 判断下列逻辑语句的 True、False
1) 1 > 1 or 3 > 4 or 4 > 5 and 2> 1 and 9 > 8 or 7 < 6
计算过程:
# 1 > 1 or 3 > 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# 1 > 1 or 3 > 4 or F and 9 > 8 or 7 < 6
# 1 > 1 or 3 > 4 or F or 7 < 6
# F or F or 7 < 6
# F or 7 < 6
# F 2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
计算过程:
# F and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# F or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# F or F and 9 > 8 or 7 < 6
# F or F or 7 < 6
# F or 7 < 6
# F 3) 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
计算过程:
# 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
# F or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
# F or F or 9 < 8 and 4 > 6 or 3 < 2
# F or F or F or 3 < 2
# F or F or 3 < 2
# F or 3 < 2
# F 2. 求出下列逻辑语句的值。 1) 8 or 3 and 4 or 2 and 0 or 9 and 7
计算过程:
# 8 or 3 and 4 or 2 and 0 or 9 and 7
# 8 or 4 or 0 or 7
# 8 or 0 or 7
# 8 or 7
# 2) 0 or 2 and 3 and 4 or 6 and 0 or 3
计算过程:
# 0 or 2 and 3 and 4 or 6 and 0 or 3
# 0 or 3 and 4 or 6 and 0 or 3
# 0 or 4 or 6 and 0 or 3
# 0 or 4 or 0 or 3
# 4 or 0 or 3
# 4 or 3
# 3) 5 and 9 or 10 and 2 or 3 and 5 or 4 or 5
计算过程:
# 5 and 9 or 10 and 2 or 3 and 5 or 4 or 5
# 9 or 10 and 2 or 3 and 5 or 4 or 5
# 9 or 2 or 3 and 5 or 4 or 5
# 9 or 2 or 5 or 4 or 5
# 9 or 5 or 4 or 5
# 9 or 4 or 5
# 9 or 5
# 3. 下列结果是什么?
提示:
(1)牢记优先级:not > and > or (2)and 取值 从前往后,
(a) 第一个数的bool值为 False,那就直接返回第一个数(0就返回0,False就返回False);
(b) 如果第一个数的bool值为 True,第二个数的bool值为 False,则返回第二个数的值;
(c) 如果两个数的bool值都为True,则返回第二个数.
(d) 如果两个数的bool值都为False,则返回第一个数. (3)or 取值
从前往后,
(a) 第一个数的bool值为 False,那就直接返回第二个数;
(b) 如果第一个数的bool值为 True,第二个数的bool值为 False,则返回第一个数的值;
(c) 如果两个数的bool值都为True,则返回第一个数.
(d) 如果两个数的bool值都为False,则返回第二个数. 1) 6 or 2 > 1
计算过程:
print(6 or 2 > 1)
# 6 or 2 > 1
# 6 or T
# 2) 3 or 2 > 1
计算过程:
print(6 or 2 > 1)
# 6 or 2 > 1
# 6 or T
# 3) 0 or 5 < 4
计算过程:
print(0 or 5 < 4)
# 0 or 5 < 4
# 0 or F
# False 4) 5 < 4 or 3
计算过程:
print(5 < 4 or 3)
# 5 < 4 or 3
# F or 3
# 5) 2 > 1 or 6
计算过程:
print(2 > 1 or 6)
# 2 > 1 or 6
# T or 6
# T 6) 3 and 2 > 1
计算过程:
print(3 and 2 > 1)
# 3 and 2 > 1
# 3 and T
# T 7) 0 and 3 > 1
计算过程:
print(0 and 3 > 1)
# 0 and 3 > 1
# 0 and T
# 8) 2 > 1 and 3
计算过程:
print(2 > 1 and 3)
# 2 > 1 and 3
# T and 3
# 9) 3 > 1 and 0
计算过程:
print(3 > 1 and 0)
# 3 > 1 and 0
# T and 0
# 10) 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
计算过程:
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)
# 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
# T and 2 or 2 < 3 and 3 and 4 or 3 > 2
# 2 or 2 < 3 and 3 and 4 or 3 > 2
# 2 or F and 3 and 4 or 3 > 2
# 2 or F and 4 or 3 > 2
# 2 or F or 3 > 2
# 2 or 3 > 2
# 2 or T
# 4. 写代码:计算 1-2+3...+99 中除了88意外所有数的总和? num = 0
count = 0
while count < 100:
count += 1 # count = count + 1 必须写在 continue 之前,否则进入死循环
if count == 88:
continue
if count % 2: # 获取奇数
num += count
else:
num -= count
print(num)

作业题答案及分析过程

[ python ] 字符串的操作及作业题的更多相关文章

  1. Python字符串切片操作知识详解

    Python字符串切片操作知识详解 这篇文章主要介绍了Python中字符串切片操作 的相关资料,需要的朋友可以参考下 一:取字符串中第几个字符 print "Hello"[0] 表 ...

  2. Python 字符串大小写操作

    #coding=utf-8 #python中字符串的操作 # 字符串的大小写 s='hello_wOrld_oF_you' upper_str = s.upper() print('全部大写: ',u ...

  3. Python字符串的操作

    字符串常用操作 name = "my name is alex" # 注: python中方法名前后带下划线的是供内部使用的方法, 如方法__dir__(). 这种方法是不对外提供 ...

  4. day7 python字符串的操作及方法

    1.字符串 1.1 字符串的操作 # 1.字符串的拼接 strvar = "我爱" + "中国" # 2.字符串的重复 strvar = "今天下午2 ...

  5. Python—字符串的操作

    字符串的操作 变量: 变量只能是 字母,数字或下划线的任意组合,但首个字符不能为数字,且不能有空格 以下关键字不能声明为变量: and ,as, assert, break ,class ,conti ...

  6. python字符串的操作(去掉空格strip(),切片,查找,连接join(),分割split(),转换首字母大写, 转换字母大小写...)

    #可变变量:list, 字典#不可变变量:元祖,字符串字符串的操作(去掉空格, 切片, 查找, 连接, 分割, 转换首字母大写, 转换字母大小写, 判断是否是数字字母, 成员运算符(in / not ...

  7. python字符串常见操作

    字符串常见操作 如有字符串mystr = 'hello world itcast and itcastcpp',以下是常见的操作 <1>find 检测 str 是否包含在 mystr中,如 ...

  8. 初学Python——字符串相关操作

    基本字符串操作 Pyhton中字符串的格式化输出在前面已经总结了,接下来介绍一些常用的字符串操作 先定义一个字符变量,以下的操作都以此为例: name=" my name is china ...

  9. Python字符串基础操作

    ==============字符串======== >>> s1='www.baidu.com' >>> type(s1) <type 'str'> & ...

随机推荐

  1. jQuery绑定事件

    1.事件绑定的方式 事件 DOM:三种绑定方式 jQuery: #前面几种内部调用的全是on $('.c1').click() $('.c1').blur() $('.c1').aaaaa() $(' ...

  2. 【题解】CF#713 E-Sonya Partymaker

    这题真的想了挺久的,然而到最后也还是没想到怎样处理环的情况……网上竟然也完全没有题解,无奈之下到 CF 的 AC 代码里面去找了一份膜拜了一下.感谢~ 由于觉得这题有一定的难度,自己看代码也看了比较久 ...

  3. 【刷题】洛谷 P3796 【模板】AC自动机(加强版)

    题目描述 有 \(N\) 个由小写字母组成的模式串以及一个文本串 \(T\) .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串 \(T\) 中出现的次数最多. 输入输出格式 输入格式 ...

  4. Coding and Paper Letter(四十五)

    资源整理. 1 Coding: 1.Python库gempy,一种基于Python的开源三维结构地质建模软件,它允许从界面和方向数据隐式(即自动)创建复杂的地质模型. 它还支持随机建模以解决参数和模型 ...

  5. Java 工作2年后需要达到怎么样的技术水平

    有人回答说这只能是大企业或者互联网企业的工程师才能拿到.也许是的,小公司或者非互联网企业拿两万的不太可能是码农了,应该是已经转管理后才有可能.还有区域问题,这个不在我的考虑范围内,因为除了北上广深杭, ...

  6. openssl生成https证书、转换证书格式的各种相关操作

    第一步:生成 private key.csr等文件 我们可能需要输入以下信息(交互式): --- Country Name (2 letter code) [AU]:US State or Provi ...

  7. python使用pwd和grp操作unix用户及用户组

    1.pwd模块 pwd模块提供了一个unix密码数据库即/etc/passwd的操作接口,这个数据库包含本地机器用户帐户信息 常用操作如下: pwd.getpwuid(uid):返回对应uid的示例信 ...

  8. mysql数据库使用sql命令窗口查询的数据,改成sql语句导入到mysql数据库中

    1.查询语句为select * from t_table;导出的数据格式如下: 2.将数据文本备份,然后使用NOTEPAD++打开,然后只拷贝数据到新建txt中,然后进行如下替换: 1)将“ | ”分 ...

  9. js绑定事件和解绑事件

    在js中绑定多个事件用到的是两个方法:attachEvent和addEventListener,但是这两个方法又存在差异性 attachEvent方法  只支持IE678,不兼容其他浏览器 addEv ...

  10. 线程池-Threadlocal

    ThreadLoclc初衷是线程并发时,解决变量共享问题,但是由于过度设计,比如弱引用的和哈希碰撞,导致理解难度大.使用成本高,反而成为故障高发点,容易出现内存泄露,脏数据.贡献对象更新等问题.单从T ...