python-12 数据结构 字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/L1558198727/article/details/83014851

一般操作

>>> str1="hello"
>>> 2*str1
'hellohello'
>>> min(str1)
'e'

编码和解码

>>> str1 = "编码和解码"
>>> str1
'编码和解码'
>>> b1 = str1.encode(encoding="cp936")
>>> b1
b'\xb1\xe0\xc2\xeb\xba\xcd\xbd\xe2\xc2\xeb'
>>> b1.decode(encoding="cp936")
'编码和解码'

不指定默认是utf-8

格式化?

>>> "结果为%f"%88
'结果为88.000000'

>>> "%s  %d  %f"%("ddd",90,12.3)
'ddd  90  12.300000'

字典
>>> "%(lang)d"%{"lang":2323}
'2323'

字符串格式化?
%a
%s
%r

format

>>> format(85,"0.5f")
'85.00000'

>>> format(0.5,"%")
'50.000000%'

>>> "{2},{1},{0}".format("a","b","c")
'c,b,a'

>>> str.format_map("{name:s},{age:d}",{"name":"tom","age":20})
'tom,20'

猜你喜欢

转载自blog.csdn.net/L1558198727/article/details/83014851
今日推荐