pythonmax对字符_(MAX第五篇)Python--字符串操作(三)
字符串操作(三)
此篇總結(jié)包含字符串的替換、轉(zhuǎn)換以及字符串格式化
替換或調(diào)整字符串
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)
返回一個(gè)原字符串居中,并使用空格填充至長度 width 的新字符串
>>> Name='Max'
>>> Name.center(30)#共30個(gè)字符,居中,其他則為空格
' Max '
>>> Name.center(30,'*')
'*************Max**************'#共30個(gè)字符,居中,其他則為*
>>> Name.center(30,'-')
'-------------Max--------------'#共30個(gè)字符,居中,其他則為-
Code
Return
string.ljust(width)
返回一個(gè)原字符串左對齊,并使用空格填充至長度 width 的新字符
string.rjust(width)
返回一個(gè)原字符串右對齊,并使用空格填充至長度 width 的新字符串
>>> Name='Max'
>>> Name.ljust(20) #左對齊,余下為空格
'Max '
>>> Name.rjust(20,'-') #右對齊,前面用-補(bǔ)齊
'-----------------Max'
>>> Name.ljust(20,'*') #左對齊,余下為*
'Max*****************'
Code
Return
string.zfill(width)
返回長度為 width 的字符串,原字符串 string 右對齊,前面填充0
>>> Name.zfill(10)
'0000000Max'
字符串格式化
字符串格式化就是把一個(gè)數(shù)值插入到字符串中的特定的位置。
Code
Describe
%c
格式化字符及其ASCII碼
%s
格式化字符串
%d
格式化整數(shù)
%f
格式化浮點(diǎn)數(shù)字,可指定小數(shù)點(diǎn)后的精度
%e
用科學(xué)計(jì)數(shù)法格式化浮點(diǎn)數(shù)
%u
格式化無符號整型
%o
格式化無符號八進(jìn)制數(shù)
%x
格式化無符號十六進(jìn)制數(shù)
%X
格式化無符號十六進(jìn)制數(shù)
>>> print('My name is %s' % 'Max') #%s對應(yīng)‘Max’字符串,‘Max’前的%是必須家在替換的字符串前面
My name is Max
>>> print('My name is %s, and my age is %d' %('Max',18)) #‘Max’替換 %s, 18替換 %d。多個(gè)替換需要括起來
My name is Max, and my age is 18
字符串format()格式化
基本愈發(fā)是通過 { } 和 :來代替之前的 %。
format()函數(shù)可以接受多個(gè)參數(shù),位置也不限定。
>>> print('My name is {}, my age is {}'.format('Max',18))
#不指定位置,按照順序把’Max'傳遞給第一個(gè){},18傳遞給第二個(gè){}.
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'傳遞給第一個(gè){},18傳遞給第二個(gè){},'ZZU'傳遞給第三個(gè){}.
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'傳遞給第三個(gè){},18傳遞給第一個(gè){},'ZZU'傳遞給第一個(gè){}.
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)
#按照賦值的變量參數(shù)進(jìn)行格式化。
'My name is Max,my age is 18'
>>> 'My name is {}, my age is {}'.format('Max',{18})
#format后面括號里的參數(shù)用大括號{},則輸出攜帶大括號
'My name is Max, my age is {18}'
str.format()格式化數(shù)字的方法
{:}
{:.2f}表示保留兩位小數(shù),2可以換成n, 如果是{:.0f}則近似到個(gè)位數(shù)
>>>'The number is {:.2f}.'.format(3.1415926)
'The number is 3.14.'
>>>'The number is {:.2f}.'.format(3.1415926)
'The number is 3.'
{:+.3f}帶符號保留,冒號后面的是數(shù)字所添加的符號,3表示保留的小數(shù)位
>>>'The number is {:+.2f}.'.format(3.1415926)
'The number is +3.142.'
{:.3%},小數(shù)變成百分?jǐn)?shù),百分號前面的數(shù)保留兩位三位小數(shù)
>>> 'The number is {:.3%}.'.format(0.1415926)
'The number is 14.159%.'
{:,},以逗號隔開的數(shù)字格式
>>> 'The number is {:,}.'.format(123456789)
'The number is 123,456,789.'
{:.3e},指數(shù)的形式,保留三位小數(shù)
>>> 'The number is {:.3e}.'.format(123456789)
'The number is 1.235e+08.'
常用的不同進(jìn)制的格式化
Code
base
'{:b}'.format(num)
二進(jìn)制
'{:d}'.format(num)
十進(jìn)制
'{: o}'.format(num)
八進(jìn)制
'{:x}'.format(num)
16進(jìn)制
>>> 'The number is {:b}.'.format(14)#二進(jìn)制
'The number is 1110.'
>>> 'The number is {:o}.'.format(14)#八進(jìn)制
'The number is 16.'
>>> 'The number is {:d}.'.format(14)#十進(jìn)制
'The number is 14.'
>>> 'The number is {:x}.'.format(14)#十六進(jìn)制
'The number is e.'
格式化同樣有數(shù)字的對齊形式,類似于string.center(), string,rjust(), string.ljust().
^,分別表示居中,左對齊,右對其,后面可以加是參數(shù)寬度,冒號后面可以帶填充的字符,不過僅支持一個(gè)字符(任意字符),這個(gè)字符串的對齊一樣,如果不帶則默認(rèn)填充空格。
>>> 'The number is {:-<5d}.'.format(123) #左對齊,寬度為5,-補(bǔ)齊
'The number is 123--.'
>>> 'The number is {:-》5d}.'.format(123)
'The number is --123.' #右對齊,寬度為5,-補(bǔ)齊
>>> 'The number is {:*<8d}.'.format(1234)#左對齊,剩下補(bǔ)*
'The number is 1234****.'
>>> 'The number is {:x^8d}.'.format(1234) #居中對其,剩下補(bǔ)x
'The number is xx1234xx.'
>>> 'The number is {:m^10d}.'.format(312213) #居中對其,剩下補(bǔ)m
'The number is mm312213mm.'
總結(jié)
以上是生活随笔為你收集整理的pythonmax对字符_(MAX第五篇)Python--字符串操作(三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: range python 3.6 typ
- 下一篇: 互动整合营销_今天,我们谈谈展会的整合营