Python3學習筆記 | 五、Python的類型與運算-字元串(下)

  • 2019 年 10 月 6 日
  • 筆記

一、字元串方法

除表達式之外,字元串還提供了一系列的方法去實現更複雜的文本處理任務。方法是與特定的對象相關聯在一起的函數。從技術角度來講,它們附屬於對象的屬性,而這些屬性不過是些調用函數罷了。在Python中,表達式和內置函數可能在不同範圍的類型有效,但方法通常特定於對象類型,例如,字元串方法僅適用於字元串對象。

屬性讀取: Object.attribute格式的表達式可以理解為「讀取object對象的屬性attribute值」

函數調用表達式: 具有函數(參數)格式的表達式意味著「調用函數程式碼,傳遞零或者更多用逗號隔開的參數對象,最後返回函數的返回值」。

這兩者合併可以讓我們調用一個對象方法。

有如下幾種字元串方法: 大小寫相關: • S.capitalize()/S.title()/S.upper()/S.lower()/S.swapcase() 排版相關: • S.center(width[, fill])/ S.ljust(width[, fill])/S.rjust(width[, fill])/ S.expandtabs(tabsize)/S.zfill(width) • S.strip([chars])/S.lstrip([chars])/S.rstrip([chars]) 查找相關: • S.startswith(prefix[, start[, end]])/S.endswith(suffix[ ,start[ ,end]])/ S.count(sub[, start[, end]]) • S.find(sub [, start[, end]])/S.rfind(sub[, start[, end]])/S.replace(old, new[, count]) • S.index(sub[, start[, end]])/S.rindex(sub[, start[, end]]) 格式判斷: • S.isalpha()/S.isdigit()/S.isalnum()/S.islower/S.isupper()/S.isspace()/S.istitle() • S.isdecimal()/S.isnumeric()/S.isidentifier()/S.isprintable() - Python3.x 字元分隔: • S.split([sep[, maxsplit]])/S.rsplit([sep[, maxsplit]])/S.splitlines([keepends])/S.partition(sep)/S.rpartition(sep) 其他: • S.join(iterable) • S.maketrans(x[, y[, z]])/S.translate(map) - Python3.x • S.format()

1、大小寫

S.capitalize() - 第一個字母大寫,其餘都變成小寫字母。

>>> 'Dora EmON'.capitalize()  'Dora emon'

S.title() - 每一詞的頭字母大寫,其餘都變成小寫字母。

>>> 'Dora EmON'.title()  'Dora Emon'

S.upper() - 全改成大寫

>>> 'Dora EmON'.upper()  'DORA EMON'

S.lower() - 全改成小寫

>>> 'Dora EmON'.lower()  'dora emon'

S.swapcase() - 大小寫轉換

>>> 'Dora EmON'.swapcase()  'dORA eMon'

2、排版

S.center(width[, fill])/ S.ljust(width[, fill])/S.rjust(width[, fill]) - 原來的字元擴到指定長度,再用fill來填充其餘空處,默認空格。中間對齊/左對齊/右對齊。

>>> 'DoraEmon'.center(15)  '    DoraEmon   '  >>> 'DoraEmon'.center(15,'十')  '十十十十DoraEmon十十十'  >>> 'DoraEmon'.ljust(15)  'DoraEmon  >>> 'DoraEmon'.rjust(15,'十')  '十十十十十十十DoraEmon'

S.expandtabs(tabsize) - 更改tabs(在字元串里」t」)轉換為空格的數量,默認為8。

>>> 'DoratEmon'.expandtabs(8)  'Dora    Emon'  >>> 'DoratEmon'.expandtabs(16)  'Dora            Emon'

S.zfill(width) - 原來的字元串擴到指定長度,前面使用0來填充,數字計算的時候會用到。

>>> 'DoraEmon'.zfill(15)  '0000000DoraEmon'

S.strip([chars])/S.lstrip([chars])/S.rstrip([chars]) - 刪除兩遍/左邊/右邊的指定字元(默認為空格和換行符)。

>>> ' DoraEmonn '.strip()  'DoraEmon'  >>> ' DoraEmonn '.lstrip()  'DoraEmonn '  >>> ' DoraEmonn '.rstrip()  ' DoraEmon'  >>> ' DoraEmonn '.strip('Em')  ' DoraEmonn '

3、查找

在這裡所有的start和start, end看成是是S[start:]和S[start:end]

S.startswith(prefix[, start[, end]])/S.endswith(suffix[ ,start[ ,end]])- 以特定字元串開頭/結尾的,返回真假。

>>> 'DoraEmon'.startswith('D')  True  >>> 'DoraEmon'.startswith('D',2,3)  False  >>> 'DoraEmon'.endswith('o',6,7)  True

S.count(sub[, start[, end]])- 相應字元串在文本里的個數。

>>> 'DoraEmon'.count('o')  2  >>> 'DoraEmon'.count('o',1,5)  1

S.find(sub [, start[, end]])/S.rfind(sub[, start[, end]])- 查找第一個與sub匹配的字元串位置,沒找到返回-1。

>>> 'DoraEmon'.find('D')  0  >>> 'DoraEmon'.find('E')  4  >>> 'DoraEmon'.find('E',3,5)  4  >>> 'DoraEmon'.find('Q')  -1

S.index(sub[, start[, end]])/S.rindex(sub[, start[, end]])- 與find差不多,但沒找到會返回錯誤。

>>> 'DoraEmon'.index('D')  0  >>> 'DoraEmon'.index('E')  4  >>> 'DoraEmon'.index('E',3,5)  4  >>> 'DoraEmon'.index('Q')  Traceback (most recent call last):   File "<stdin>", line 1, in <module>  ValueError: substring not found

S.replace(old, new[, count])- 查找old,替換成new,count是只替換相應個數,默認全替換。

>>> 'DoraEmon'.replace('o','Xiong',)  'DXiongraEmXiongn'  >>> 'DoraEmon'.replace('o','Xiong',1)  'DXiongraEmon'

4、格式判斷

返回的值都是真或者假。 S.isalpha() - 判斷是不是字母 S.isdigit() - 判斷是否數字 S.isalnum() - 判斷是否數字和字母 S.islower() - 判斷是不是有字母而且是全小寫 S.isupper() - 判斷是不是有字母而且是全大寫 S.isspace() - 判斷是不是只有空格和換行符號 S.istitle() - 判斷每個單詞第一個字母是不是大寫

>>> 'DoraEmon'.isalpha()  True  >>> 'DoraEmon'.isdigit()  False  >>> 'DoraEmon'.isalnum()  True  >>> 'DoraEmon'.islower()  False  >>> 'DoraEmon'.isupper()  False  >>> 'DoraEmon'.isspace()  False  >>> 'DoraEmon'.istitle()  False

下面的只有Python3.x開始支援 S.isdecimal() - 判斷是不是數字 S.isnumeric() - 判斷是不是數字 S.isidentifier() - 判斷字元能否成為標識符 S.isprintable() - 判斷字元是否全部是能列印(print)的

>>> 'DoraEmon'.isdecimal()  False  >>> 'DoraEmon'.isnumeric()  False  >>> 'DoraEmon'.isidentifier()  True  >>> 'DoraEmon'.isprintable()  True

5、字元分割

S.split([sep[, maxsplit]])/S.rsplit([sep[, maxsplit]])- 從左/右以sep(字元串)分隔,最多maxsplit次(默認無限)

>>> 'DoraEmon'.split('o')  ['D', 'raEm', 'n']  >>> 'DoraEmon'.split('o',1)  ['D', 'raEmon']  >>> 'DoraEmon'.rsplit('o',1)  ['DoraEm', 'n']  

S.splitlines([keepends])- 以n或者r或者rn分隔

>>> 'DnorraEmornn'.splitlines()  ['D', 'o', 'raEmo', 'n']

S.partition(sep)/S.rpartition(sep)- 從左/右找第一個sep分隔,sep左邊的,sep,sep右邊的。

>>> 'DoraEmon'.partition('o')  ('D', 'o', 'raEmon')  >>> 'DoraEmon'.rpartition('o')  ('DoraEm', 'o', 'n')

6、其他

S.join(iterable) S.maketrans(x[, y[, z]])/S.translate(map)-創建對照表, 然後使用translate()函數, 調用對照表, 把字元串(string)中的字元, 進行相應的替換,這個在python2.x和python3.x完全不同。 S.format-字元串格式化方法,後面會詳細講解

>>> '-'.join('DoraEmon')  'D-o-r-a-E-m-o-n'  >>>  >>> D='DoraEmon'  >>> X=D.maketrans('Dora','DaXo')  >>> D.translate(X)'DaXoEman'  >>>  >>> 'DoraEmon{DA}'.format(DA=' And DaXiong')  'DoraEmon And DaXiong'

二、字元串格式化表達式

這個格式化表達式與C語言的差不多。

>>> '%d%s%s4'%(1,'2','Three')  '12Three4'

格式是字元串里有%[(name)][flag][width][.precision]typecode加一個%, 再加上相應個數的元組(後面會講元組)

1、格式化程式碼(typecode)

s 字元串(或任何對象) r 與s一樣,但輸出方式是repr方式,而不是str c 字元 d 十進位(整數) i 整數 u 無號整數 o 八進位整數 x 十六進位整數 X 與x同樣,A-F是大寫 e 浮點指數 E 與e同樣,E是大寫 f 浮點數十進位 F 浮點數十進位 g 浮點e或f G 浮點E或F

2、例子

>>> "%s | %r | %c" %("This is string","This is repr","C")  "This is string | 'This is repr' | C"  >>> "%d | %i | %o | %x | %x"%(3,5,11,13,15)  '3 | 5 | 13 | d | f'  >>> "%e | %E | %f | %F | %g | %G" %(1.5E3,1.5e3,13.5,13.5,1.5E13,13.5e15)  '1.500000e+03 | 1.500000E+03 | 13.500000 | 13.500000 | 1.5e+13 | 1.35E+16'  >>> "%(string)-10s" %({'string':'1'})  '1  >>> "%(float)+10.2f" % ({'float':3.1})  '     +3.10'  >>> "%(float)-10.2f" % ({'float':3.1})  '3.10

三、字元串格式化方法

在Python字元串方法里有format方法,根據特定的格式在裡面輸入相應關鍵字。例: 『What do you like, {0}, {1} or {2}』.format(「spam」, 「eggs」, 「ham」) 結果是』What do you like, spam, eggs, ham』 也可以換順序 : 『What do you like, {2}, {1} or {0}』.format(「spam」, 「eggs」, 「ham」) 結果是』What do you like, ham, eggs, spam』 也可以使用相應的名字 『What do you like, {ham}, {0} or {eggs}』.format(「spam」, eggs=」eggs」, ham=」ham」) 結果是』What do you like, ham, spam, eggs』 具體格式為: {fieldname!conversionflag:formatspec} fieldname是位置資訊或關鍵字,後面可以跟屬性或指針 conversionflag是r或者s,分別對應repr和str formatspec是具體顯示方式,可以替代前面所使用過的其他字元串方法

fieldtype 使用屬性或指針

>>> import sys  >>> sys.platform  'win32'  >>> "{0.platform}".format(sys)  'win32'  >>> "{0[Dora]}".format({"Dora":"DoraEmon"})  'DoraEmon'  >>> dict1={"Dora":"DoraEmon"}  #關於字典,後面會講到。  >>> dict1["Dora"]  'DoraEmon'

當使用字典的時候需要引號來擴,但使用字元串格式化方法的時候不能使用引號

formatspec = [[fill]align][sign][#][0][width][,][.precision][type],fill和align再加上後面的width相當於方法里的center,ljust,rjust

>>> "{:*^40}".format("start")  '*****************start******************'  >>> "{:*^40}".format("end")  '******************end*******************'

這裡也是,fill必須是單個字元。 align有<左對齊,>右對齊,^中間顯示,=,在數字里符號在最前,數字右對齊。

>>> print("{:=10}n{:=+10}n{:-^10}n{:=+10}".format(10,3,'-',13))          10  +        3  ----------  +       13

sign的值是+,-和空格。當+的時候,即使是正數,也會顯示符號,-是只有在負數時顯示符號(默認值),空格時,會為正數前面留下符號位

>>> "{:+}".format(10)  '+10'  >>> "{:-}".format(10)  '10'  >>> "{:-}".format(-10)  '-10'  >>> "{}".format(10)  '10'  >>> "{:}".format(10)  '10'

只有在數字顯示里,顯示二進位數,八進位數,十六進位數的時候,需要顯示前面的0b,0o,0x的時候使用。

>>> "{0:#8b},{0:#8o},{0:#8x}".format(10)  '  0b1010,    0o12,     0xa'  >>> "{:,}".format(10000000)  '10,000,000'  >>> "{:08.5}".format(13.5767)  '0013.577'

type跟之前使用%表示的相等。當字元時:使用s,默認就是s;當整數時:b,o,x和X是二進位、八進位、十六進位,c是數字按unicode轉成字元,d是正常十進位,默認是d。也可以使用n來代替d。

>>> "{0:d},{0:b},{0:o},{0:x},{0:X}".format(10)  '10,1010,12,a,A'

浮點數時:e和E是指數,f和F是浮點數。g和G是同一的,也可以使用n來代替g,%是顯示百分比。

>>> "{0:e},{0:F},{0:g},{0:n},{0:%}".format(1.1)  '1.100000e+00,1.100000,1.1,1.1,110.000000%'

Tags: