所謂內置方法,就是凡是字元串都能用的方法,這個方法在創建字元串的類中,下麵是總結: 首先,我們要學習一個獲取幫助的內置函數 help(對象) ,對象可以是一個我們創建出來的,也可以是創建對象的那個類,類也是一個對象,被稱為類對象。 當我們進入解釋器的交互模式中輸入以下代碼時: 其中,str就是創建字 ...
所謂內置方法,就是凡是字元串都能用的方法,這個方法在創建字元串的類中,下麵是總結:
首先,我們要學習一個獲取幫助的內置函數 help(對象) ,對象可以是一個我們創建出來的,也可以是創建對象的那個類,類也是一個對象,被稱為類對象。
當我們進入解釋器的交互模式中輸入以下代碼時:
help(str)
其中,str就是創建字元串的類,然後我們就會得到一長串的結果:
Help on class str in module __builtin__: class str(basestring) | str(object='') -> string | | Return a nice string representation of the object. | If the argument is a string, the return value is the same object. | | Method resolution order: | str | basestring | object | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y #字元串拼接,看+號就知道 | | __contains__(...) | x.__contains__(y) <==> y in x #判斷x里字元是否在y里 | | __eq__(...) | x.__eq__(y) <==> x==y | | __format__(...) | S.__format__(format_spec) -> string | | Return a formatted version of S as described by format_spec. | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__('name') <==> x.name #獲取屬性 | | __getitem__(...) | x.__getitem__(y) <==> x[y] #索引取值,詳情參考python中的序列 | | __getnewargs__(...) | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] #切片,也是序列的一種方法 | | Use of negative indices is not supported. | | __gt__(...) | x.__gt__(y) <==> x>y | | __hash__(...) | x.__hash__() <==> hash(x) | | __le__(...) | x.__le__(y) <==> x<=y | | __len__(...) | x.__len__() <==> len(x) | | __lt__(...) | x.__lt__(y) <==> x<y | | __mod__(...) | x.__mod__(y) <==> x%y | | __mul__(...) | x.__mul__(n) <==> x*n | | __ne__(...) | x.__ne__(y) <==> x!=y | | __repr__(...) | x.__repr__() <==> repr(x) | | __rmod__(...) | x.__rmod__(y) <==> y%x | | __rmul__(...) | x.__rmul__(n) <==> n*x | | __sizeof__(...) | S.__sizeof__() -> size of S in memory, in bytes #用位元組表示在記憶體中的大小 | | __str__(...) | x.__str__() <==> str(x) | | capitalize(...) | S.capitalize() -> string | '''返迴首字母大寫字元串副本,對中文無效''' | Return a copy of the string S with only its first character | capitalized. | | center(...) | S.center(width[, fillchar]) -> string | '''返回指定寬度(width)的字元串副本,原字元串居中對齊,可指定用什麼來填充多餘部分(fillchar)預設為空格,關於對齊和填充可以看上篇博文的解釋''' | Return S centered in a string of length width. Padding is | done using the specified fill character (default is a space) | | count(...) | S.count(sub[, start[, end]]) -> int | '''計數器,返回給定字元串在原字元串出現的次數,也可指定範圍''' | Return the number of non-overlapping occurrences of substring sub in | string S[start:end]. Optional arguments start and end are interpreted | as in slice notation. | | decode(...) | S.decode([encoding[,errors]]) -> object | '''解碼,將字元串解碼成某種字元集''' | Decodes S using the codec registered for encoding. encoding defaults | to the default encoding. errors may be given to set a different error | handling scheme. Default is 'strict' meaning that encoding errors raise | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' | as well as any other name registered with codecs.register_error that is | able to handle UnicodeDecodeErrors. | | encode(...) | S.encode([encoding[,errors]]) -> object | '''編碼,將字元串編碼''' | Encodes S using the codec registered for encoding. encoding defaults | to the default encoding. errors may be given to set a different error | handling scheme. Default is 'strict' meaning that encoding errors raise | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and | 'xmlcharrefreplace' as well as any other name registered with | codecs.register_error that is able to handle UnicodeEncodeErrors. | | endswith(...) | S.endswith(suffix[, start[, end]]) -> bool | '''判斷字元串在某範圍內(範圍用索引指定,不指定預設是整個字元串)是否以指定的字元串(suffix)結尾,返回布爾值''' | Return True if S ends with the specified suffix, False otherwise. | With optional start, test S beginning at that position. | With optional end, stop comparing S at that position. | suffix can also be a tuple of strings to try. | | expandtabs(...) | S.expandtabs([tabsize]) -> string | '''將字元串里的製表符(一般用tab建輸入,也可以手動使用特殊字元 \t )轉換成空格,預設一個製表符轉換成8個空格,也可以指定個數(tabsize),並返回一個轉換後的副本''' | Return a copy of S where all tab characters are expanded using spaces. | If tabsize is not given, a tab size of 8 characters is assumed. | | find(...) | S.find(sub [,start [,end]]) -> int | '''在原字元串一定範圍內,尋找給定的字元串,找到了返回第一個被找到的字元的索引值,沒找到就返回-1''' | Return the lowest index in S where substring sub is found, | such that sub is contained within S[start:end]. Optional | arguments start and end are interpreted as in slice notation. | | Return -1 on failure. | | format(...) | S.format(*args, **kwargs) -> string | '''字元串格式化(比%s更為高級,需要的話自行去瞭解),(*args, **kwargs)是處理函數傳參的一種方式,以後繼續講''' | Return a formatted version of S, using substitutions from args and kwargs. | The substitutions are identified by braces ('{' and '}'). | | index(...) | S.index(sub [,start [,end]]) -> int | '''和S.find()作用一樣,只不過沒找到時會報錯''' | Like S.find() but raise ValueError when the substring is not found. | | isalnum(...) | S.isalnum() -> bool | '''判斷字元串內是否由字母和數字組成,返回布爾值''' | Return True if all characters in S are alphanumeric | and there is at least one character in S, False otherwise. | | isalpha(...) | S.isalpha() -> bool | '''判斷字元串是否全是由字母組成,返回布爾值''' | Return True if all characters in S are alphabetic | and there is at least one character in S, False otherwise. | | isdigit(...) | S.isdigit() -> bool | '''判斷字元串是否全是由數字組成,返回布爾值''' | Return True if all characters in S are digits | and there is at least one character in S, False otherwise. | | islower(...) | S.islower() -> bool | '''判斷字元串里的所有字母是否都是小寫,當然,前提是字元串裡面最少有一個字母,返回布爾值''' | Return True if all cased characters in S are lowercase and there is | at least one cased character in S, False otherwise. | | isspace(...) | S.isspace() -> bool | '''判斷字元串是否都是由空白字元組成(空格),當然,前提是字元串裡面最少有一個空格,返回布爾值''' | Return True if all characters in S are whitespace | and there is at least one character in S, False otherwise. | | istitle(...) | S.istitle() -> bool | '''判斷字元串是否是標題,而標題的標準就是所有單詞的首字母都是大寫,其他的都是小寫,返回布爾值''' | Return True if S is a titlecased string and there is at least one | character in S, i.e. uppercase characters may only follow uncased | characters and lowercase characters only cased ones. Return False | otherwise. | | isupper(...) | S.isupper() -> bool | '''判斷字元串里的所有字母是否都是大寫,和小寫對應''' | Return True if all cased characters in S are uppercase and there is | at least one cased character in S, False otherwise. | | join(...) | S.join(iterable) -> string | '''字元串的拼接,詳情看我前面的博文''' | Return a string which is the concatenation of the strings in the | iterable. The separator between elements is S. | | ljust(...) | S.ljust(width[, fillchar]) -> string | '''左對齊,關於對齊和填充可以看我上篇博文''' | Return S left-justified in a string of length width. Padding is | done using the specified fill character (default is a space). | | lower(...) | S.lower() -> string | '''返回一個全是小寫的字元串副本''' | Return a copy of the string S converted to lowercase. | | lstrip(...) | S.lstrip([chars]) -> string or unicode | '''和strip類似,不過只去除左邊的空格,可以指定字元''' | Return a copy of the string S with leading whitespace removed. | If chars is given and not None, remove characters in chars instead. | If chars is unicode, S will be converted to unicode before stripping | | partition(...) | S.partition(sep) -> (head, sep, tail) | '''和 split 的分隔類似,但返回的是元祖,不過 split 不過保留給定字元,translate 則會保留,並放在中間,沒找到則前後為空''' | Search for the separator sep in S, and return the part before it, | the separator itself, and the part after it. If the separator is not | found, return S and two empty strings. | | replace(...) | S.replace(old, new[, count]) -> string | '''替換,用新的字元串(new),替換原字元串里有的老字元串(old),用 count 指定替換的次數,不指定則全部替換''' | Return a copy of string S with all occurrences of substring | old replaced by new. If the optional argument count is | given, only the first count occurrences are replaced. | | rfind(...) | S.rfind(sub [,start [,end]]) -> int | '''和S.find()作用類似,不過尋找方向是從右向左''' | Return the highest index in S where substring sub is found, | such that sub is contained within S[start:end]. Optional | arguments start and end are interpreted as in slice notation. | | Return -1 on failure. | | rindex(...) | S.rindex(sub [,start [,end]]) -> int | '''和S.index()作用類似,不過尋找方向是從右向左''' | Like S.rfind() but raise ValueError when the substring is not found. | | rjust(...) | S.rjust(width[, fillchar]) -> string | '''右對齊''' | Return S right-justified in a string of length width. Padding is | done using the specified fill character (default is a space) | | rpartition(...) | S.rpartition(sep) -> (head, sep, tail) | '''partition 的右邊操作版本''' | Search for the separator sep in S, starting at the end of S, and return | the part before it, the separator itself, and the part after it. If the | separator is not found, return two empty strings and S. | | rsplit(...) | S.rsplit([sep [,maxsplit]]) -> list of strings | '''split 的右邊操作版,要設置了 maxsplit 才能體現,否則都是全部分隔''' | Return a list of the words in the string S, using sep as the | delimiter string, starting at the end of the string and working | to the front. If maxsplit is given, at most maxsplit splits are | done. If sep is not specified or is None, any whitespace string | is a separator. | | rstrip(...) | S.rstrip([chars]) -> string or unicode | '''和strip類似,不過只去除右邊的空格,可以指定字元''' | Return a copy of the string S with trailing whitespace removed. | If chars is given and not None, remove characters in chars instead. | If chars is unicode, S will be converted to unicode before stripping | | split(...) | S.split([sep [,maxsplit]]) -> list of strings | '''按照給定的字元進行分割(從左邊開始,找到的第一個),返回一個分割由後剩下的字元串組成的列表(不保留sep), | maxsplit 指定最大分割次數,否則凡是出現指定的分隔符都會分隔''' | Return a list of the words in the string S, using sep as the | delimiter string. If maxsplit is given, at most maxsplit | splits are done. If sep is not specified or is None, any | whitespace string is a separator and empty strings are removed | from the result. | | splitlines(...) | S.splitlines(keepends=False) -> list of strings | '''根據換行符分割''' | Return a list of the lines in S, breaking at line boundaries. | Line breaks are not included in the resulting list unless keepends | is given and true. | | startswith(...) | S.startswith(prefix[, start[, end]]) -> bool | '''判斷是否已指定字元串開頭,與上面的結尾判斷相對應''' | Return True if S starts with the specified prefix, False otherwise. | With optional start, test S beginning at that position. | With optional end, stop comparing S at that position. | prefix can also be a tuple of strings to try. | | strip(...) | S.strip([chars]) -> string or unicode | '''與 center 的填充相反,這裡是移除兩邊的填充,同樣也可以指定移除填充的字元,預設是空格,和 center 類似''' | Return a copy of the string S with leading and trailing | whitespace removed. | If chars is given and not None, remove characters in chars instead. | If chars is unicode, S will be converted to unicode before stripping | | swapcase(...) | S.swapcase() -> string | '''返回一個翻轉原字元串字母大小寫後的副本''' | Return a copy of the string S with uppercase characters | converted to lowercase and vice versa. | | title(...) | S.title() -> string | '''將字元串轉換為標題格式''' | Return a titlecased version of S, i.e. words start with uppercase | characters, all remaining cased characters have lowercase. | | translate(...) | S.translate(table [,deletechars]) -> string | '''根據參數table給出的表(翻譯表,翻譯表是通過maketrans方法轉換而來)轉換字元串的字元, 要過濾掉的字元放到 del 參數中''' | Return a copy of the string S, where all characters occurring | in the optional argument deletechars are removed, and the | remaining characters have been mapped through the given | translation table, which must be a string of length 256 or None. | If the table argument is None, no translation is applied and | the operation simply removes the characters in deletechars. | | upper(...) | S.upper() -> string | '''將字元串的字母全部大寫''' | Return a copy of the string S converted to uppercase. | | zfill(...) | S.zfill(width) -> string | '''返回一個給定長度的字元串(小於原字元串無效),原字元右對齊,前面用0填充''' | Pad a numeric string S with zeros on the left, to fill a field | of the specified width. The string S is never truncated. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T字元串內置方法
首先,我們要註意一個問題,所以的方法都沒有改變原字元串本身,都是返回了一個新對象,具體原理我會在講到函數中說明,而一個新的對象沒有把它賦值給一個變數的話,其引用數就為0,在python進行垃圾回收的時候,就會將其從記憶體中清除。
另外,如果你英文夠好的話,其實使用 help() 函數就能夠自行學習了,所以這裡對幫助函數是一些要點進行說明:
1. <==> 表示相對於,意思這前後的方法效果是一樣的
2. -> 表示函數的返回值,意思是這個方法處理以後,返回的值是什麼類型,可以是字元串 string ,也可以是數字 int 等等。關於返回值的詳細,會在講函數的時候分析。
3.函數進行傳值的時候,對傳入的值的類型是有要求的,不然會有很多報錯,但這裡並沒有明說一定要傳什麼類型的值,而已在英文說明中隱含,所以需要一定的英語閱讀能力,英文不好就用經驗來堆吧。
首先,我先來說說對於字元串來說,各運算符的含義:
1.+
代表字元串拼接,不多講了
2.in
表示給定的字元串是否在原字元串裡面,返回布爾值
3.==
判斷兩個字元串是否一樣,值相等就行,返回布爾值
4.===
判斷是否是統一對象,不僅值要相同,在記憶體中的地址也有一樣,返回布爾值
5.!=
不等於,值和對象都不相等,返回布爾值
5.<,>,<=,>=
字元串的大小判斷非常奇特,它是用每個字元逐一比較,比較的是字元對應的ascll編碼,例如:
a = 'a' #以十進位的ascll為例,其為97 b = 'b' #以十進位的ascll為例,其為98 a < b
另外,其是每個字元逐一比較的,一旦某個字元比另一個大,則整個字元串都大於另一個,例如
a只有一個字元,但比較的時候,是用 a 的第一個字元 'z' 和 b的第一個字元 'a' 比較,因為'z' > 'a' 了,所以整個字元串都大。如果逐一比較時,兩個字元相等的話,就比較下一個字元,如果比較到最後都相等,則說明兩個字元串的值相等(==)。至於是否是同一對象就需要另外確定。
6.*
字元串的乘法將會返回一個多出重覆原字元串的副本。
只能和數字相乘,字元串間相乘是不可以的。
也沒有什麼“乘法分配率”的說法,這樣只是重覆元祖而已。
7.%
取模運算符就是字元串格式化時使用的符號。
關於剩下的內置方法,我會另起一篇進行總結分析。