在字符串的最前面加r或者R 可以让字符串中所有的转义字符的功能消失(每个符号逗号编程普通字符)
str1 r C:Usersnametestdemo.py print(str1) # C:Usersnametestdemo.py字符串格式化 1.格式化字符串
语法 包含格式占位符的字符串 % (数据1, 数据2, …)
1 %s - 字符串占位符%s可以给任何类型的数据占位
str1 %sxx % 你 print(str1) # 你xx str1 %s-xxx % [10, 20] print(str1) # [10, 20]-xxx2 %d - 整数占位符
%d可以给任何数字数据占位
str1 %d-xxx % 23 print(str1) # 23-xxx str1 %d-xxx % 12.5 print(str1) # 12-xxx3 %f - 浮点数占位符(保留六位小数)
%f可以给任何数字数据占位
str1 %fxx % 4.34 print(str1) # 4.340000xx str1 余额:%.2f % 3452233.1515 print(str1) # 余额:3452233.152.f-string 语法 f’{任何有结果的表达式}’ - 用表达式的结果来填充{}所在的位置加参数 .Nf - 控制小数位数, - 银行金额分隔的形式.N% - 将数转换成百分率 比值保留N位小数x N 将数据转换成长度为N的字符串 不够的在前面用x对应的字符来填充x N 将数据转换成长度为N的字符串 不够的在后面用x对应的字符来填充 1 基本用法
str1 f {name}-{age}-{age * 10}-{name[-1]}
print(str1) # 小明-18-180-明
2 参数
a. .Nf
money 2305
str1 f 金额 {money:.2f}
print(str1) # 金额 2305.00
b. ,
money 2349000
str1 f 余额 {money:,}
print(str1) # 余额 2,349,000
c. .N%
rate 0.5
str1 f 时长占有率 {rate:.2%}
print(str1) # 时长占有率 50.00%
d. x N x N
、 - 决定填充的时候填在前面还是后面
x - 填充字符
N - 目标长度
num 9
str1 f Python2106{num:0 3}
print(str1) # Python2106009
str1 f Python2106{num:0 3}
print(str1) # Python2106900
相关函数
1.center、rjust、ljust、zfill
字符串.center(长度, 填充字符) 例如 ‘abc’.center(7, ‘x’)– ‘xxabcxx’字符串.rjust(长度, 填充字符) 例如 ‘abc’.rjust(7, ‘x’)– ‘xxxxabc’字符串.ljust(长度, 填充字符) 例如 ‘abc’.ljust(7, ‘x’)– ‘abcxxxx’字符串.zfill(长度) 字符串.rjust(长度, ‘0’) 例如 ‘abc’.zfill(7)– ‘0000abc’
print( abc .center(7, x )) # xxabcxx print( abc .rjust(7, x )) # xxxxabc print( abc .ljust(7, x )) # abcxxxx print( abc .zfill(7)) # 0000abc2.count 字符串1.count(字符串2) - 统计字符串1中字符串2出现的次数字符串1.count(字符串2, 开始下标, 结束下标) 统计在字符串1中 开始下标到结束下标(取不到)对应的范围内统计字符串2出现的次数
str1 how are you? u am fine, thank you! and you? print(str1.count( a )) # 4 print(str1.count( you )) # 3 print(str1.count( a , 0, 12)) # 13.find/rfind、index/rindex 字符串1.find(字符串2) - 获取字符串1中字符串2第一次出现的位置 返回0开始的下标值 如果字符串2不存在 返回-1字符串1.index(字符串2) - 获取字符串1中字符串2第一次出现的位置 返回0开始的下标值 如果字符串2不存在 报错字符串1.find(字符串2, 开始下标, 结束下标)、字符串1.find(字符串2, 开始下标, 结束下标)
str1 how are you? i am fine, thank you! and you? str1 how are you? i am fine, thank you! and you? print(str1.find( you )) # 8 print(str1.rfind( you )) # 39 print(str1.index( you )) # 8 print(str1.rindex( you )) # 39 print(str1.find( you1 )) # -1 # print(str1.index( you1 )) # 报错 print(str1.find( a , 4, 12)) # 4 print(str1.index( y , 3, 25)) # 84.isdigit()、isnumeric() 字符串.isdigit() - 判断是否是纯数字字符串(数字指0~9的数字字符)字符串.isnumeric() - 判断是否是纯数字字符串(数字是具有数字意义的字符)
print( 234234 .isdigit()) # True print( 234234 .isnumeric()) # True print( 23一十百千万壹贰⑤ .isnumeric()) # True5.islower、isupper 字符串.islower() - 判断字符串是否是纯小写字母字符串字符串.upper() - 判断字符串是否是纯大写字母字符串
print( asda .islower()) # True print( SFAF .isupper()) # True6.join 字符串.join(序列) - 将序列中的 元素用指定的字符串拼接成一个新的字符串 注意 序列中的元素必须是字符串
result .join( abc ) print(result) # a b c result .join([ name , 小明 ]) print(result) # name 小明 result .join([ name , 小明 ]) print(result) # name小明
练习 将列表中的元素合并成一个字符串
[‘abc’, 100, 12.5, True, ‘hello’] - ‘abc10012.5Truehello’
list1 [ abc , 100, 12.5, True, hello ] result .join([str(i) for i in list1]) # abc10012.5Truehello
练习2 已经一个列表保存多个学生的分数 提取所有学生的姓名 以一个字符串的形式返回
[{‘name’:‘张三’, ‘age’: 18}, {‘name’:‘小明’, ‘age’: 20}, {‘name’:‘小花’, ‘age’: 30}] - ‘张三,小明,小花’
list1 [{ name : 张三 , age : 18}, { name : 小明 , age : 20}, { name : 小花 , age : 30}]
result , .join([str(stu[ name ]) for stu in list1]) # 张三,小明,小花
7.strip、lstrip、rstrip
字符串.strip() - 去掉字符串两端的空白字符字符串.lstrip() - 去掉字符串前面的空白字符字符串.rstrip() - 去掉字符串后面的空白字符
str1 tabc i 123 n result str1.strip() print( , result, , sep ) result str1.lstrip() print( , result, , sep ) result str1.rstrip() print( , result, , sep )8.replace 字符串1.replace(字符串2, 字符串3) - 将字符串1中所有的字符串2替换成字符串3字符串1.replace(字符串2, 字符串3, N) - 将字符串1中前N个字符串2替换成字符串3
str1 how are you? i am fine, thank you! and you? result str1.replace( u , b , 2) print(result) # how are yob? i am fine, thank yob! and you?9.maketrans、translate
str.maketrans(字符串1, 字符串2) - 创建一张字符串1和字符串2的字符对应表
table str.maketrans( ain , 你我他 ) # a-你, i-我, n-他 result str1.translate(table) print(result) # how 你re you? 我 你m f我他e, th你他k you! 你他d you?
练习 将字符串中所有的阿拉伯数字都替换成对应的中文数字
‘123木头人 88’ - ‘一二三木头人 八八’
table str.maketrans( 0123456789 , 零一二三四五六七八九 ) str1 123木头人 88 result str1.translate(table) print(result) # 一二三木头人 八八10.split
字符串1.split(字符串2) - 将字符串1中所有的字符串2作为切割点对字符串进行切割
str1 how are you? i am fine, thank you! and you? result str1.split( you ) print(result) # [ how are , ? i am fine, thank , ! and , ? ]注意 如果切割点在最前面或者最后面或者出现练习的切割点 结果会出现空串
str2 amnayouaa a123aklpa print(str2.split()) # [ , mn , you , , , 123 , klp , ]



