python学习之字符串常用方法和格式化字符串

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'))

输出:6
str='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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

转载于:https://www.cnblogs.com/SunIan/p/9719465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值