Python為String類型提供了很多很有用的內置方法,這篇文章主要針對Python2.7的內置方法做一個測試列舉,展示一下用途。
如果大家想看原版的,可以去這個網址看(https://docs.python.org/2/library/stdtypes.html#string-methods),但是這里是我自己的實踐以及一些理解。
1. str.
capitalize
()
返回第一個字母大寫的str
str = "a string" str.capitalize()
'A string'
2.str.
center
(width[, fillchar])
返回一個width寬的string,其中str居中顯示,如果有多余的字符,則以fillchar來填充,若沒有指定fillchar,則默認使用空格。這個方法主要的功能在於輸出一致長度的內容,這邊舉一個小的應用:
icon = "*" for i in range(1,11,2): if i<=5: out = icon * i else: out = icon print out.center(5)
執行上述代碼,則會輸出如下的一顆樹:
3. str.
count
(sub[, start[, end]])
返回sub在str內部從start到end為止(不包括end),不重疊出現的次數
a= "abcdefabcdabcccccc" a.count("a") 3 a.count("c",0,12) 2 a="aaaaaaaaaa" a.count("aa") 5
4.str.
decode
([encoding[, errors]])
在Python中,從Unicode轉為其他編碼方式成為encode,反之成為decode。所以這個方法返回將用encoding編碼的str恢復為unicode,輸出是一個unicode的字符串。errors是對於解碼過程中出錯時候的處理方式,主要有以下幾種
5.str.
encode
([encoding[, errors]])
將str用encoding的編碼方式轉為字符串。errors和上述表格一致。
現在將4,5 這兩個方法結合在一起展示一個例子:
1 # -*- coding: utf-8 -*- 2 3 a = "中文" 4 #a.decode("utf-8") 5 a = a.decode("utf-8") 6 #a = a.decode() 7 print a 8 a.encode("utf-8") 9 print a
代碼第一行:在python源碼中如果使用了中文字符,運行時會有錯誤,解決的辦法是在源碼的開頭部分加入字符編碼的聲明,在自帶的編輯器內如果不加的話在會出現如下的報錯彈出框:
第5行指定了encoding的參數為"utf-8", 則print a的時候可以正常輸出中文兩個字,如果是第6行這樣不指定的,使用的是默認的編碼方式,在Python2的自帶編輯器內,是ASCII。那么ASCII是無法編碼中文的,所以運行時在shell內會輸出報錯,第8行將unicode又重新編碼回普通的string,輸出中文兩個字。
關於Python中的編碼解碼問題可以看一下這個鏈接:https://my.oschina.net/leejun2005/blog/74430
6.str.
endswith
(suffix[, start[, end]])
判斷 str是不是以suffix結尾,start和end指定開始和結尾,不包括end所在的字符。suffix可以是一個tuple,包含多個suffix
a = "abcbcb##" print a.endswith("#") print a.endswith("#",0,-3)
輸出
True
False
7.str.
expandtabs
([tabsize])
根據指定的tabsize來擴展str中的tab,tabsize的默認值是8
這邊引用一個stack overflow(https://stackoverflow.com/questions/34546171/python-expandtabs-string-operation)上的一段代碼來作為例子:
>>> str = "this is\tstring" >>> print str.expandtabs(0) this isstring >>> print str.expandtabs(1) this is string >>> print str.expandtabs(2) this is string >>> print str.expandtabs(3) this is string >>> print str.expandtabs(4) this is string >>> print str.expandtabs(5) this is string >>> print str.expandtabs(6) this is string >>> print str.expandtabs(7) this is string >>> print str.expandtabs(8) this is string >>> print str.expandtabs(9) this is string >>> print str.expandtabs(10) this is string >>> print str.expandtabs(11) this is string
8. str.
find
(sub[, start[, end]])
find 可以找到sub在str(start和end不包括end之間)內的index。如果要判斷一個sub是不是在str中,用in
9. str.
format
(*args, **kwargs)
str中可以用{numeric},或者是{key} 來指代要被格式化的部分,然后在format中,用tuple或者是dict來給str中的那些部分賦值
>>> a = "this is a {0} made by {1}".format("test","tina") >>> a 'this is a test made by tina' >>> a = "this is a {mode} made by {name}".format(mode = "test",name = "tina") >>> a 'this is a test made by tina'
10. str.
index
(sub[, start[, end]])
和find一樣,獲取sub在str中的index,但是如果找不到的話,會raise valueError()
11. str.
isalnum
()
判斷str是不是只包含數字或者字母,並且長度至少為1,如果是的話則返回True,否則False
>>> a= "123" >>> a.isalnum() True >>> a = "123abc" >>> a.isalnum() True >>> a = "abc" >>> a.isalnum() True >>> a = "" >>> a.isalnum() False >>> a = "+-^&*(123" >>> a.isalnum() False >>> a = "1" >>> a.isalnum() True >>> a = "a" >>> a.isalnum() True
12. str.
isalpha
()
如果str中只包含字母,並且長度大於1,則返回True,否則返回False
>>> a="" >>> a.isalpha() False >>> a="a" >>> a.isalpha() True >>> a = "1" >>> a.isalpha() False
13. str.
isdigit
()
判斷str中是否只有數字並且長度大於等於1,是的話返回True,否則返回False
>>> a="" >>> a.isdigit() False >>> a="a" >>> a.isdigit() False >>> a = "1" >>> a.isdigit() True
14. str.
islower
()
如果str中全是小寫字符並且str至少包含一個小寫字母,則返回True,否則返回False
15. str.
isspace
()
如果str中全是空格並且str的長度大於等於1,則返回True,否則返回False
16. str.
istitle
()
如果str中的每個word都首字母大寫而且str至少包含一個大寫字母,則返回True,否則返回False
>>> a = "this is a test string" >>> a.istitle() False
17. str.
isupper
()
如果str中的字母都是大寫,並且str中至少包含一個大寫字母 ,則返回True,否則返回False
>>> a= "123" >>> a.isupper() False >>> a = "123A" >>> a.isupper() True >>> a="abc" >>> a.isupper() False
18. str.
join
(iterable)
iterable是一個序列的迭代器,如果這個序列包含了string和unicode之外的類型,則join會出錯,如果序列中包含了unicode,則最后返回一個unicode。這個方法將序列中的每個元素用str來連接起來
>>> a = [1,2,3,4,5] >>> ','.join(a) Traceback (most recent call last): File "<pyshell#93>", line 1, in <module> ','.join(a) TypeError: sequence item 0: expected string, int found >>> a = ['a','b','c'] >>> ','.join(a) 'a,b,c'
19. str.
ljust
(width[, fillchar])
返回一個width長的string,其中str左對齊,剩余的位置用fillchar補齊,fillchar默認是空格。如果width<len(str) ,則返回str
>>> a = "if you know what i mean" >>> a.ljust(40,'*') 'if you know what i mean*****************' >>> a.ljust(40) 'if you know what i mean ' >>> a.ljust(2) 'if you know what i mean'
20. str.
lower
()
將str中的字母都換為小寫字母再返回,數字無所謂大寫小寫,會保持不變
21. str.
lstrip
([chars])
從左邊開始,將str中的chars的位置刪除,直到遇到第一個不符合的位置為止,如果不提供chars,則刪除空格。如果從左邊開始的第一個字符不符合chars中的任何一個,則不會刪除任何字符
>>> a = ' this is a test ' >>> a.lstrip() 'this is a test ' >>> a.lstrip('a') ' this is a test ' >>> a.lstrip('t') ' this is a test ' >>> a = 'www.example.com' >>> a.lstrip('a') 'www.example.com' >>> a.lstrip('cmowz.') 'example.com'
22. str.
partition
(sep)
將str用sep分隔,並且返回一個包含三個元素的Tuple,即sep之前的部分,sep 和sep之后的部分; 如果在str中沒有找到sep,則返回str和兩個空的string組成的元組
23. str.
replace
(old, new[, count])
將str中old的部分用new來替換,如果count這個參數也提供了,那么只有前count個old會被替換
>>> a = "aaaaaaa" >>> a.replace('a','t') 'ttttttt' >>> a.replace('a','t',3) 'tttaaaa'
24. str.
rfind
(sub[, start[, end]])
返回str的start 和end(不包括end)之間包含了sub的最大的index,如果沒找到則返回-1
25. str.
rindex
(sub[, start[, end]])
和rfind的功能一致,但是如果沒有找到會報valueError的錯
26. str.
rjust
(width[, fillchar])
返回一個width寬的string,其中str居右對齊,如果有多余的字符位置,則用fillchar補齊;如果width<len(str),則返回str。fillchar的默認值是一個空格
27. str.
rpartition
(sep)
將str在sep最后一次出現的位置分隔,並且返回一個包含三個元素的Tuple,即sep之前的部分,sep 和sep之后的部分; 如果在str中沒有找到sep,則返回兩個空的string和str組成的元組
28. str.
rsplit
([sep[, maxsplit]])
用sep作為分隔符分割str,返回一個包含分割之后各個詞的list。如果sep沒有提供,則用空格來分割。如果制定了maxsplit,則只有右側的maxsplit個詞被分割出來。和split的區別只有一個是從左,一個是從右。
>>> a = 'this.is a test.for the rsplit' >>> a.rsplit(' ') ['this.is', 'a', 'test.for', 'the', 'rsplit'] >>> a.rsplit(' ',2) ['this.is a test.for', 'the', 'rsplit'] >>> a.rsplit(' ',1) ['this.is a test.for the', 'rsplit']
29. str.
rstrip
([chars])
從右邊開始,將str中的chars的位置刪除,直到遇到第一個不符合的位置為止,如果不提供chars,則刪除空格。如果從右邊開始的第一個字符不符合chars中的任何一個,則不會刪除任何字符
30. str.
split
([sep[, maxsplit]])
用sep作為分隔符分割str,返回一個包含分割之后各個詞的list。如果sep沒有提供,則用空格來分割。如果制定了maxsplit,則只有左側的maxsplit個詞被分割出來。連續的spe不會作為一個sep來處理,而是當做sep之間是一個空的字符,如下所示
>>> a.split(',') ['1', '', '2']
sep可以包括多個字符,如‘<>’。如果sep沒有指定的話,則用空格來作為分隔符,並且連續的空格被認定為一個,如下所示:
>>> a=' ' >>> a.split() [] >>> a = 'this is a test code for split with whitespace' >>> a.split() ['this', 'is', 'a', 'test', 'code', 'for', 'split', 'with', 'whitespace']
31. str.
splitlines
([keepends])
根據換行標志來分割string,'\r' , '\n', '\r\n' 是常用的換行符(boundaries)
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', '']
32. unicode.
splitlines
([keepends])
與上一個string的splitlines基本一致,除了unicode使用的換行符更多一些,如下表所示:
33. str.
startswith
(prefix[, start[, end]])
判斷 str是不是以suffix開始,start和end指定開始和結尾,不包括end所在的字符。prefix可以是一個tuple,包含多個prefix
34. str.
strip
([chars])
刪除str中包含的chars,如果chars沒有提供,則刪除str中的空格。chars可以是一個字符串,包含多個要刪除的字符
35. str.
swapcase
()
將str中的大寫字母和小寫字母反轉,即大寫改小寫,小寫改大寫
36. str.
title
()
將str轉為title格式
>>> a = 'this is a title' >>> a.title() 'This Is A Title'
37. str.
translate
(table[, deletechars])
刪除str中的deletechars,並且將剩余的字符用table的對應關系來對應為另一個字符。可以用maketrans來生成一個翻譯的table,也可以指定為None,當這個table指定為None時,這個方法只是簡單地執行刪除字符的操作。
38. str.
upper
()
將str中的字符轉為大寫格式。需要注意一點,str.upper().isupper() 可能為False,比如當str中含有數字字符時,或者當Unicode
39. str.
zfill
(width)
返回一個width長的string,如果str的長度小於width,則在左側填充0,如果str的長度大於width,則返回str
>>> a='this' >>> a.zfill(10) '000000this'
40. unicode.
isnumeric
()
如果unicode內部只有數字,則返回True,否則返回False。 Numeric指數字字符以及Unicode中的數字類型,如 U+2155, VULGAR FRACTION ONE FIFTH.
>>> u = u'1.2345' >>> u.isnumeric() False >>> u=u'12345' >>> u.isnumeric() True
41. unicode.
isdecimal
()
如果unicode內部只有數字,則返回True,否則返回False。 Numeric指數字字符以及Unicode中的數字類型,如 U+2155, VULGAR FRACTION ONE FIFTH.
isnumeric 和 isdecimal 的區別詳見這個鏈接:http://www.runoob.com/python/att-string-isnumeric.html,最后的筆記列表有較為詳細的介紹。
如果有問題的地方,歡迎大家批評指正。