pythonmax对字符_(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.'
總結
以上是生活随笔為你收集整理的pythonmax对字符_(MAX第五篇)Python--字符串操作(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: range python 3.6 typ
- 下一篇: 互动整合营销_今天,我们谈谈展会的整合营