Python中的字符串同样适用标准的序列操作(索引,分片,乘法,成员判断,求长度,取最小值和最大值),但因为字符串是不可变的,因此字符串不支持分片赋值。
1 s='http://www.baidu.com'
2 s[-3:]='aaa'
3 print(s)输出结果:
1 s[-3:]='aaa'
2 TypeError: 'str' object does not support item assignment可以看出抛出的错误信息,字符串不允许标记内部项。
但我们可以在字符串中用一个百分比符号%s标记出一个占位符,它表示我们将要在该位置插入转换值的位置。s将会被格式化为字符串,如果被转换的对象不是字符串,则会将其转换为字符串。
除了用%s插入转换值外,还可以使用substitute模板方法,用传递进来的关键字参数替换字符串中的关键字。
1 from string import Template
2 s=Template('$x,glorious $b')
3 s=s.substitute(x='slurm',b='haha')
4 print(s)输出结果:
slurm,glorious haha我们看到$s位置被替换为slurm,$b位置被替换为haha
如果被替换的位置是单词的一部分,可以将其用{}括起来
1 from string import Template
2 s=Template('${x}glorious $b')
3 s=s.substitute(x='slurm',b='haha')
4 print输出结果:
slurmglorious haha使用字典变量提供值得/名称对替换
1 from string import Template
2 s=Template('$name come from $county ')
3 d={}
4 d['name']='zhangsan'
5 d['county']='china'
6 s=s.substitute(d)
7 print(s)输出结果:
zhangsan come from china 1 s='%s come from %s'%('zhangsan','china')
2 print(s)输出结果:
1 zhangsan come from china转换类型 | 解释 |
|---|---|
d,i | 带符号的十进制整数 |
o | 不带符号的八进制 |
u | 不带符号的十进制 |
x | 不带符号的十六进制 |
e | 科学计数法表示的浮点数(小写) |
E | 科学计数法表示浮点数(大写) |
f.F | 十进制浮点数 |
c | 单字符 |
r | 字符串(用repr转换任意Python对象) |
s | 字符串(用str转换任意python对象) |
字符串与utf8互转
1 s='你好'
2 print(s.encode('utf8'))
3 a=s.encode('utf8')
4 print(a.decode('utf8'))输出结果:
1 b'\xe4\xbd\xa0\xe5\xa5\xbd'
2 你好宽度是指转换后的值所保留的最小字符个数,精度则是结果中应该包含的小数位数
例如 输出宽度为10的pi的值
1 from math import pi
2 p='%10f'%pi
3 for k, i in enumerate(p) : #使用enumerate函数打印序列
4 print('序列%s'%k,i) 1 序列0
2 序列1
3 序列2 3
4 序列3 .
5 序列4 1
6 序列5 4
7 序列6 1
8 序列7 5
9 序列8 9
10 序列9 3打印精度为2的pi的值
1 from math import pi
2 p='%.2f'%pi
3 print(p)输出结果:
3.14打印宽度为10,精度为2的pi的值
1 from math import pi
2 p='%10.2f'%pi
3 for k,i in enumerate(p):
4 print('序列%s 打印值%s'%(k,i))打印结果:
1 序列0 打印值
2 序列1 打印值
3 序列2 打印值
4 序列3 打印值
5 序列4 打印值
6 序列5 打印值
7 序列6 打印值3
8 序列7 打印值.
9 序列8 打印值1
10 序列9 打印值4我们看到,当整数部分没有值时,将以空' ' 代替。
1 print('%+5d'%10)
2 print('%+5d'%-10)输出:
1 +10
2 -10使用字符串格式化,使我们的代码看着更简洁
1 width=input('>>输入宽度:')
2 price_with=10
3 item_width=int(width)-price_with
4 header_format='%-*s%*s'
5 format='%-*s%*.2f'
6 print('='*int(width))
7 print(header_format%(item_width,'item',price_with,'price'))
8 print('_'*int(width))
9 print(format%(item_width,'apple',price_with,0.4))
10 print(format%(item_width,'Pears',price_with,0.5))输出结果:
>>输入宽度:30
==============================
item price
______________________________
apple 0.40
Pears 0.50 方法名 | 解释 | 案例 |
|---|---|---|
find | 在一个长的字符串中查找字符串,返回字符串所在位置的最左端的索引,如果没有则返回-1 | str='hello world'print(str.find('world'))输出:6str='hello world'print(str.find('worldd'))输出:-1 |
join | 用来连接列表中的字符串 | l=['1','2','3','4','5','6']sep='+'ret=sep.join(l)print(ret)输出:1+2+3+4+5+6 |
lower | 返回字符串的小写母版(忽略用户大小写时使用,例如,用户输入用户名含有大写字母,输入后将其转换为小写并与数据库中的保存字符匹配) | str='HELLO WORLD'print(str.lower())输出:hello world |
replace | 返回字符串中所有被匹配项被替换后的所得到的新字符串 | str='HELLO WORLD'print(str.lower().replace('world','python'))输出:hello python |
split | 按某个分隔符将字符串分割成序列,默认以空格符分割 | str='1+2+3+4'print(str.split('+'))输出结果:['1', '2', '3', '4']str='HELLO WORLD'print(str.split())输出结果:['HELLO', 'WORLD'] |
strip | 去除字符串两边的空格 | str=' HELLO WORLD 'print(str.strip())输出结果:HELLO WORLD |
maketrans | 创建字符映射的转换表,接收两个参数,第一个参数是字符串,表示要转换的字符串,第二个参数也是字符串表示转换的目标(两个参数是映射关系(一一对映),因此长度必须相同) | intab = "el"outtab = "EL"trantab = str.maketrans(intab, outtab)str = "hello world"print (str.translate(trantab))输出:hELLo worLd |