Day8-字符串
01-字符串操作
-
in 和 not in
# 字符串1 in 字符串2 - 判断字符串1是否是字符串2的子串(判断字符串2中是否包含字符串1) print('a' in 'abc') print('ab' in 'abc')
-
相关函数(len、str)
-
求长度
str1 = 'sdfnkflsdfslfk' print(len(str1))
-
str(数据) - 将数据转换成字符串
-
那些能转:所有数据都可以转换成字符串
-
怎么转:在数据打印值外面加引号
print(str(True)) print(str([1,2,3]))
-
字符串转数字
str6 = '783' print(int(str6)) str7 = '12.5' print(float(str7)) str8 = '3e4' print(float(str8))
-
-
02-格式化字符串
-
格式字符串
格式字符串:在字符串中用格式占位符代替字符串变化的部分,然后在后面用数据来给格式占位符赋值
-
语法
带有格式占位符的字符串 % (数据1,数据2,数据3,…)
-
说明
- % - 固定写法
- () - 固定写法,在数据只有一个可以省略
- 数据 - 数据的个数和前面占位符保持一致
- 格式占位符
- %s - 字符串
- %d - 整数
- %f - 浮点数;%.Nf - 浮点数保留N位小数
- %c - 字符/字符编码值
message = '%s今年%d岁,月薪%.2f元, 等级:%c' % (name, age, money, 65) print('格式字符串:', message)
-
-
f-string
-
format方法
包含{}的字符串.format(数据1,数据2,…) - 字符串中{}就相当于格式字符串中的占位符
-
基本用法
-
.{}
message = '{}今年{}岁,月薪{}元'.format(name, age, money) print('format1:', message)
-
.下标
message = '{1}的年龄:{0}, 今年{0}岁'.format(age, name) print('format2:', message)
-
.{key}
message = '{y}的年龄:{x}, 今年{x}岁'.format(x=age, y=name) print('format3:', message)
-
-
格式约束:{:约束条件}、{下标:约束条件}、{key:约束条件}
- .Nf - 保留N位小数(四舍五入)
- 符号>Nd - 约束数据宽度为N,不够的前面用指定符号填充
- 符号<Nd
- , - 将数字三位一组用逗号隔开
- % - 将小数转换成百分比数据,保留6位小数
- .N% - 将小数转换成百分比数据,保留N位小数
print('约束1:{:.2f}'.format(3.1415926)) # 约束1:3.14 print('约束1:{0:.3f}'.format(3.1415926)) # 约束1:3.142 print('约束2:{:0>5}'.format(100)) # 约束2:00100 print('约束2:{:0<5}'.format(23)) # 约束2:23000 print('约束2:{: >5}'.format('abc')) # 约束2: 34 print('约束3:{:,}'.format(1000000)) # 约束3:1,000,000 print('约束4:{:%}'.format(0.2356)) # 约束4:23.560000% print('约束4:{:.1%}'.format(0.2356)) # 约束4:23.6%
-
f-string - format方法的简写
message = f'{name}今年{age}岁,月薪:{money}元!' print(message) # 小明今年23岁,月薪:78273.982元! message = f'{name*2}今年{age+10}, 月薪:{money/1000}K' print(message) # 添加约束 print(f'pi:{3.1415926:.2f}') # pi:3.14 print(f'年龄:{age: >5}') # 年龄: 23
-
-
03-字符串的相关方法
# 1.字符串1.count(字符串2) - 统计字符串1中字符串2出现的次数
message = 'how are you? i am fine, Thank you, and you?'
print('you 的个数:', message.count('you')) # 3
print('空格的个数:', message.count(' ')) # 9
# 2.
# 字符串1.find(字符串2) - 获取字符串2第一次在字符串1中出现的位置(返回的是下标值), 如果找不到就返回-1
message = 'how are you? i am fine, Thank you, and you?'
print(message.find('you')) # 8
print(message.find('a')) # 4
print(message.find('你好')) # -1(找不到)
# 字符串1.find(字符串2,开始下标,结束下标) - 获取字符串2在字符串1指定范围内第一次出现的位置
print(message.find('you', -7)) # 39 (在'nd you?'找 'you')
print(message.find('you', 0, 10)) # -1 (在'how are yo'找 'you')
# 3.字符串.join(字符串序列) - 将序列中所有的元素用指定字符串拼接产生一个新的字符串
list1 = ['name', 'age', 'abc', '你好']
new_str = ' '.join(list1)
print(new_str) # 'nameageabc你好'
print('+'.join(list1)) # 'name+age+abc+你好'
message = 'hello'
new_str = ' '.join(message)
print(new_str) # 'h e l l o'
print('😁😢'.join(message)) # h😁😢e😁😢l😁😢l😁😢o
# 看一眼! [str(x) for x in nums] - 列表推导式
nums = [100, 20, 30, 40]
print(''.join([str(x) for x in nums])) # 100203040
# 4.
# 字符串1.replace(字符串2, 字符串3) - 将字符串1中所有的字符串2都替换成字符串3,产生一个新的字符串
# 字符串1.replace(字符串2, 字符串3, N) - 将字符串1中前N个字符串2替换成字符串3
message = 'how are you? i am fine, Thank you, and you?'
new_str = message.replace('you', 'YOU')
print(new_str) # how are YOU? i am fine, Thank YOU, and YOU?
new_str = message.replace('you', 'me', 2)
print(new_str) # how are me? i am fine, Thank me, and you?
# 5.
# 字符串1.split(字符串2) - 将字符串1中的字符串2作为切割点对字符串1进行切割
message = 'how are you? i am fine, Thank you, and you?'
print(message.split(' ')) # ['how', 'are', 'you?', 'i', 'am', 'fine,', 'Thank', 'you,', 'and', 'you?']
str1 = 'abc123abc123abc'
print(str1.split('123')) # ['abc', 'abc', 'abc']
print(str1.split('1')) # ['abc', '23abc, '23abc']
print(str1.split('b')) # ['a', 'c123a', 'c123a', 'c']
print(str1.split('c')) # ['ab', '123ab', '123ab', '']
str2 = 'abc123abcc123'
print(str2.split('c')) # ['ab', '123ab', '', '123']
# 字符串1.split(字符串2, N) - 将字符串1中的前N个字符串2作为切割点对字符串1进行切割
message = 'how are you? i am fine, Thank you, and you?'
print(message.split(' ', 2)) # ['how', 'are', 'you? i am fine, Thank you, and you?']
# 6.字符串.zfill(N) - 将字符串转换成一个指定长度的新字符串,原字符串在右边,左边用0填充
num = 34
print(str(num).zfill(4))