(MAX第五篇)Python--字符串操作(三)

字符串操作(三)

此篇总结包含字符串的替换、转换以及字符串格式化

替换或调整字符串

Code Return
string.replace(str1, str2, num=string.count(str1)) 把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次
>>> string="Nobody Nobody but you"
>>> string.replace('Nobody','Somebody',1) #替换1次
'Somebody Nobody but you'
>>> string.replace('Nobody','Somebody') #全部替换
'Somebody Somebody but you'
Code Return
string.center(width) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
>>> Name='Max'
>>> Name.center(30)#共30个字符,居中,其他则为空格
'             Max              '
>>> Name.center(30,'*')
'*************Max**************'#共30个字符,居中,其他则为*
>>> Name.center(30,'-')
'-------------Max--------------'#共30个字符,居中,其他则为-
Code Return
string.ljust(width) 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符
string.rjust(width) 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
>>> Name='Max'
>>> Name.ljust(20)  #左对齐,余下为空格
'Max                 '
>>> Name.rjust(20,'-') #右对齐,前面用-补齐
'-----------------Max'
>>> Name.ljust(20,'*') #左对齐,余下为*
'Max*****************'
Code Return
string.zfill(width) 返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0
>>> Name.zfill(10)
'0000000Max'

字符串格式化

字符串格式化就是把一个数值插入到字符串中的特定的位置。

Code Describe
%c 格式化字符及其ASCII码
%s 格式化字符串
%d 格式化整数
%f 格式化浮点数字,可指定小数点后的精度
%e 用科学计数法格式化浮点数
%u 格式化无符号整型
%o 格式化无符号八进制数
%x 格式化无符号十六进制数
%X 格式化无符号十六进制数
>>> print('My name is %s' % 'Max') #%s对应‘Max’字符串,‘Max’前的%是必须家在替换的字符串前面
My name is Max
>>> print('My name is %s, and my age is %d' %('Max',18)) #‘Max’替换 %s, 18替换 %d。多个替换需要括起来
My name is Max, and my age is 18

字符串format()格式化

基本愈发是通过 { }来代替之前的 %
format()函数可以接受多个参数,位置也不限定。

>>> print('My name is {}, my age is {}'.format('Max',18))
#不指定位置,按照顺序把’Max'传递给第一个{},18传递给第二个{}.
My name is Max, my age is 18
>>> print('My name is {0}, my age is {1},I graduated from {2}.'.format('Max',18,'ZZU')
#按照指定顺序把’Max'传递给第一个{},18传递给第二个{},'ZZU'传递给第三个{}.
My name is Max, my age is 18, I graduated from ZZU.
>>> print('My name is {2}, my age is {0},I graduated from {1}.'.format('Max',18,'ZZU')
#按照指定顺序把’Max'传递给第三个{},18传递给第一个{},'ZZU'传递给第一个{}.
My name is ZZU, my age is Max, I graduated from 18.
>>> 'My name is {name},my age is {age}'.format(name='Max',age=18)
#按照赋值的变量参数进行格式化。
'My name is Max,my age is 18'
>>> 'My name is {}, my age is {}'.format('Max',{18})
#format后面括号里的参数用大括号{},则输出携带大括号
'My name is Max, my age is {18}'

str.format()格式化数字的方法

{:}
{:.2f}表示保留两位小数,2可以换成n, 如果是{:.0f}则近似到个位数

>>>'The number is {:.2f}.'.format(3.1415926)
'The number is 3.14.'
>>>'The number is {:.2f}.'.format(3.1415926)
'The number is 3.'

{:+.3f}带符号保留,冒号后面的是数字所添加的符号,3表示保留的小数位

>>>'The number is {:+.2f}.'.format(3.1415926)
'The number is +3.142.'

{:.3%},小数变成百分数,百分号前面的数保留两位三位小数

>>> 'The number is {:.3%}.'.format(0.1415926)
'The number is 14.159%.'

{:,},以逗号隔开的数字格式

>>> 'The number is {:,}.'.format(123456789)
'The number is 123,456,789.'

{:.3e},指数的形式,保留三位小数

>>> 'The number is {:.3e}.'.format(123456789)
'The number is 1.235e+08.'

常用的不同进制的格式化

Code base
'{:b}'.format(num) 二进制
'{:d}'.format(num) 十进制
'{: o}'.format(num) 八进制
'{:x}'.format(num) 16进制
>>> 'The number is {:b}.'.format(14)#二进制
'The number is 1110.'
>>> 'The number is {:o}.'.format(14)#八进制
'The number is 16.'
>>> 'The number is {:d}.'.format(14)#十进制
'The number is 14.'
>>> 'The number is {:x}.'.format(14)#十六进制
'The number is e.'

格式化同样有数字的对齐形式,类似于string.center(), string,rjust(), string.ljust().
^,<,>分别表示居中,左对齐,右对其,后面可以加是参数宽度,冒号后面可以带填充的字符,不过仅支持一个字符(任意字符),这个字符串的对齐一样,如果不带则默认填充空格。

>>> 'The number is {:-<5d}.'.format(123) #左对齐,宽度为5,-补齐
'The number is 123--.'
>>> 'The number is {:-》5d}.'.format(123)
'The number is --123.' #右对齐,宽度为5,-补齐
>>> 'The number is {:*<8d}.'.format(1234)#左对齐,剩下补*
'The number is 1234****.'
>>> 'The number is {:x^8d}.'.format(1234) #居中对其,剩下补x
'The number is xx1234xx.'
>>> 'The number is {:m^10d}.'.format(312213) #居中对其,剩下补m
'The number is mm312213mm.'
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容