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


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

1. 替换或调整字符串

CodeReturn
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'
CodeReturn
string.center(width)返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
>>> Name='Max'
>>> Name.center(30)#共30个字符,居中,其他则为空格
'             Max              '
>>> Name.center(30,'*')
'*************Max**************'#共30个字符,居中,其他则为*
>>> Name.center(30,'-')
'-------------Max--------------'#共30个字符,居中,其他则为-
CodeReturn
string.ljust(width)返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符
string.rjust(width)返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
>>> Name='Max'
>>> Name.ljust(20)  #左对齐,余下为空格
'Max                 '
>>> Name.rjust(20,'-') #右对齐,前面用-补齐
'-----------------Max'
>>> Name.ljust(20,'*') #左对齐,余下为*
'Max*****************'
CodeReturn
string.zfill(width)返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0
>>> Name.zfill(10)
'0000000Max'

2. 字符串格式化

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

CodeDescribe
%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

3. format()格式化

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

3.1 字符串格式化

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

3.2 数字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.'

常用的不同进制的格式化

Codebase
‘{: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.'
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值