1. capitalize(self) #字符串首字母大寫 return "" """ S.capitalize() -> str Return a capitalized version of S, i.e. make the first character have upper case and the rest lower case. 返回一個大寫版本的 S,即第一個字符大寫其他小寫。 """ 例: S = 'alex' v = S.capitalize() print(v) #結果:Alex -------------------------------------------------------------------------------------------------------------------------------- 2. casefold(self) #將全部大小變小寫,casefold更牛逼,能夠轉化德語等其餘語言... return "" """ S.casefold() -> str Return a version of S suitable for caseless comparisons. 返回一個比較適合的 S 小寫版本。 """ 例: S = 'AleX' v = S.casefold() print(v) #結果:alex -------------------------------------------------------------------------------------------------------------------------------- 3. lower(self) #將全部大小變小寫 return "" """ S.lower() -> str Return a copy of the string S converted to lowercase. 返回已轉換爲小寫字符串 S 的副本 """ 例: S = 'AleX' v = v = S.lower() print(v) #結果:alex ------------------------------------------------------------------------------------------------------------------------------- 4. center(self, width, fillchar=None) #文本居中 return "" """ S.center(width[, fillchar]) -> str Return S centered in a string of length width. Padding is done using the specified fill character (default is a space) 返回 以S爲中心 長度爲width 的字符串。使用指定的填充字符 填充(默認是空格) 參數1: 表示總長度 參數2:空白處填充的字符(長度爲1) """ 例: S = 'alex' v = S.center(10) n = S.center(10,'行') a = S.center(2,'行') b = S.center(5,'行') print(v) #結果: alex print(n) #結果:行行行alex行行行 print(a) #結果:alex print(b) #結果:行alex ------------------------------------------------------------------------------------------------------------------------------- 5. count(self, sub, start=None, end=None) #表示傳入值在字符串中出現的次數 return 0 """ 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. 返回 sub 在字符串 S [start:end] 中 非重疊出現的子串次數。可選參數的 start和end 被解釋爲切片符號。 參數1: 要查找的值(子序列) 參數2: 起始位置(索引) 參數3: 結束位置(索引) """ 例: S = "alexasdfdsafsdfasdfaaaaa" v = S.count('a') n = S.count('df') a = S.count('df',12) b = S.count('df',0,15) print(v) #結果:9 print(n) #結果:3 print(a) #結果:2 print(b) #結果:2 ------------------------------------------------------------------------------------------------------------------------------- 6. endswith(self, suffix, start=None, end=None) #是否以xx結尾 return False """ S.endswith(suffix[, start[, end]]) -> bool 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. 若是字符串S的結尾和指定的suffix相同返回True,不然返回False。可選start,測試 在S的該位置開始。可選end,中止匹配 在S的該位置。suffix能夠是一個元組去嘗試。 參數1: 要查找的值(子序列) 參數2: 起始位置(第一個字符爲1) 參數3: 結束位置(第一個字符爲1) """ 例: S = 'alex' v = S.endswith('ex') n = S.endswith(("w","x")) a = S.endswith(("w","e")) b = S.endswith(("w","a"),0,1) print(v) #結果:True print(n) #結果:True print(a) #結果:False print(b) #結果:True ------------------------------------------------------------------------------------------------------------------------------- 7. startswith(self, prefix, start=None, end=None) #是否以xx開始 return False """ 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. 若是字符串S的開始和指定的suffix相同返回True,不然返回False。可選start,測試 在S的該位置開始。可選end,中止匹配 在S的該位置。suffix能夠是一個元組去嘗試。 參數1: 要查找的值(子序列) 參數2: 起始位置(第一個字符爲1) 參數3: 結束位置(第一個字符爲1) """ 例: S = 'alex' v = S.startswith('al') n = S.startswith(("w","x")) a = S.startswith(("w","e")) b = S.startswith(("w","a"),0,1) print(v) #結果:True print(n) #結果:False print(a) #結果:True print(b) #結果:True ------------------------------------------------------------------------------------------------------------------------------- 8. def encode(self, encoding='utf-8', errors='strict') #轉換成字節 return b"" """ S.encode(encoding='utf-8', errors='strict') -> bytes Encode S using the codec registered for encoding. Default encoding is 'utf-8'. 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 can handle UnicodeEncodeErrors. """ 例: S = "李傑" v1 = S.encode(encoding='utf-8') # 字節類型 print(v1) # 結果:b'\xe6\x9d\x8e\xe6\x9d\xb0' v2 = S.encode(encoding='gbk') # 字節類型 print(v2) # 結果:b'\xc0\xee\xbd\xdc' ------------------------------------------------------------------------------------------------------------------------------- 9. expandtabs(self, tabsize=8) #找到製表符\t,進行替換(包含前面的值) return "" """ S.expandtabs(tabsize=8) -> str 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. 返回使用空格擴展全部tab字符的S的副本。若是沒有給出tabsize,一個tab製表符大小 假設爲8個字符。 """ 例: S = "al\te\tx\nalex\tuu\tkkk" v = S.expandtabs(10) print(v) 結果: al e x alex uu kkk ------------------------------------------------------------------------------------------------------------------------------- 10. find(self, sub, start=None, end=None) #找到指定子序列的索引位置:不存在返回-1 return 0 """ S.find(sub[, start[, end]]) -> int 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. 返回 在最小的索引 開始 ,字符串 S 中發現子串sub,這樣的 sub 是包含在 S[start:end]裏。可選參數start和end被解釋爲切片符號。失敗返回-1。 """ 例: S = 'alex' v = S.find('e',0,1) print(v) #結果:-1 ------------------------------------------------------------------------------------------------------------------------------- 11. index(self, sub, start=None, end=None) #找到指定子序列的索引位置 return 0 """ S.index(sub[, start[, end]]) -> int Like S.find() but raise ValueError when the substring is not found. 像S.find() 可是 當在字符串中沒有找到時 報錯。 """ 例: S = 'alex' v = S.index('e',0,1) print(v) #結果:報錯ValueError: substring not found ------------------------------------------------------------------------------------------------------------------------------- 12. format(*args, **kwargs) #字符串格式化 pass """ S.format(*args, **kwargs) -> str Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}'). 返回一個格式化的版本 S,使用args 和 kwargs替換。替換是以括號('{'和'}')鑑別。 """ 例: S = "我是:{0};年齡:{1};性別:{2}" v = S.format("李傑",19,'都行') print(v) #結果:我是:李傑;年齡:19;性別:都行 S = "我是:{name};年齡:{age};性別:{gender}" v = S.format(name='李傑',age=19,gender='隨意') print(v) #結果:我是:李傑;年齡:19;性別:都行 ------------------------------------------------------------------------------------------------------------------------------- 13. format_map(self, mapping) #字符串格式化 return "" """ S.format_map(mapping) -> str Return a formatted version of S, using substitutions from mapping. The substitutions are identified by braces ('{' and '}'). 返回一個格式化的版本 S,使用mapping替換。替換是以括號('{'和'}')鑑別。 """ 例: S = "我是:{name};年齡:{age};性別:{gender}" v = S.format_map({'name':"李傑",'age':19,'gender':'中'}) print(v) #結果:我是:李傑;年齡:19;性別:中 ------------------------------------------------------------------------------------------------------------------------------- 14. isalnum(self) #是不是數字、漢字、數字. return False """ S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise. 若是S中的全部字符都是字母數字,則至少有一個字符在S中返回True,不然爲False。 """ 例: S = 'alex8漢子' v = S.isalnum() print(v) #結果:True ------------------------------------------------------------------------------------------------------------------------------- 15. isalpha(self) #是不是字母、漢字. return False """ S.isalpha() -> bool Return True if all characters in S are alphabetic and there is at least one character in S, False otherwise. 若是S中的全部字符都是字母,則至少有一個字符在S中返回Ture,不然爲False。 """ 例: S = 'alex8漢子' v = S.isalpha() print(v) #結果:False ------------------------------------------------------------------------------------------------------------------------------- 16. isdecimal(self) #是不是阿拉伯數字 return False """ S.isdecimal() -> bool Return True if there are only decimal characters in S, False otherwise. 若是隻有十進制數的字符則返回Ture 不然False """ 例: S = '二' v = S.isdecimal() # '123' print(v) #結果:False ------------------------------------------------------------------------------------------------------------------------------- 17. isdigit(self) #是不是數字 return False """ S.isdigit() -> bool Return True if all characters in S are digits and there is at least one character in S, False otherwise. 若是S中的全部字符都是數字,而且至少有一個字符在s中返回True,不然爲False。 """ 例: S = '二' v = S.isdigit() # '123','②' print(v) #結果:False ------------------------------------------------------------------------------------------------------------------------------- 18. isnumeric(self) #是不是數字字符 return False """ S.isnumeric() -> bool Return True if there are only numeric characters in S, False otherwise. 若是S中只有數字字符,則返回True,不然爲False。 """ 例: S = '二' v = S.isnumeric() # '123','二','②' print(v) #結果:Ture ------------------------------------------------------------------------------------------------------------------------------- 19. isidentifier(self) #是不是表示符 return False """ S.isidentifier() -> bool Return True if S is a valid identifier according to the language definition. Use keyword.iskeyword() to test for reserved identifiers such as "def" and "class". 若是S是一個有效標識符,則根據語言定義返回true。使用關鍵詞。iskeyword()測試保留標識符如「def」和「class」。 """ 例: S = 'name' v = S.isidentifier() print(v) #結果:Ture ------------------------------------------------------------------------------------------------------------------------------- 20. islower(self) #是否所有是小寫 return False """ 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. 若是S中的全部字符都是小寫字母,而且在S中至少有一個字符返回true,不然爲False """ 例: S = "ALEX" v = S.islower() print(v) #結果:False ------------------------------------------------------------------------------------------------------------------------------- 21. isupper(self) #是否所有是大寫 return False """ 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. 若是S中的全部字符都是大寫,而且至少包裝一個字符在S裏返回Ture,不然爲False """ 例: S = "ALEX" v = S.isupper() print(v) #結果:True ------------------------------------------------------------------------------------------------------------------------------- 22. isprintable(self) #是否包含隱含的xx return False """ S.isprintable() -> bool Return True if all characters in S are considered printable in repr() or S is empty, False otherwise. 若是全部的字符是repr()標準打印或是空返回true,不然爲false。 """ 例: S = "釣魚要釣刀魚,\t刀魚要到島上釣" v = S.isprintable() print(v) #結果:False ------------------------------------------------------------------------------------------------------------------------------- 23. isspace(self) #是否所有是空格 return False """ S.isspace() -> bool Return True if all characters in S are whitespace and there is at least one character in S, False otherwise. 若是全部字符在S中是空格 而且至少有一個字符在S裏 ,返回true,不然爲False 。 """ 例: S = ' ' v = S.isspace() print(v) #結果:Ture ------------------------------------------------------------------------------------------------------------------------------- 24.☆☆☆☆☆ join(self, iterable) # 元素拼接(元素字符串) return "" """ S.join(iterable) -> str Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. 返回一個鏈接每一個字符的iterable。元素之間的分隔符是 S. """ 例: iterable = 'alex' S = "_" v = S.join(iterable) # 內部循環每一個元素 print(v) # 結果:a_l_e_x iterable = ['海峯','槓娘','李傑','李泉'] S = "搞" v = S.join(iterable) print(v) # 結果:海峯搞槓娘搞李傑搞李泉 ------------------------------------------------------------------------------------------------------------------------------- 25. ljust(self, width, fillchar=None) # 左填充 return "" """ S.ljust(width[, fillchar]) -> str Return S left-justified in a Unicode string of length width. Padding is done using the specified fill character (default is a space). 返回左對齊的S ,Unicode字符串的長度爲width。填充使用指定的填充字符(默認是空格)。 """ 例: S = 'alex' v = S.ljust(10,'*') print(v) #結果:alex****** ------------------------------------------------------------------------------------------------------------------------------- 26. rjust(self, width, fillchar=None) #右填充 return "" """ S.rjust(width[, fillchar]) -> str Return S right-justified in a string of length width. Padding is done using the specified fill character (default is a space). 返回右對齊的S ,Unicode字符串的長度爲width。填充使用指定的填充字符(默認是空格)。 """ 例: S = 'alex' v = S.rjust(10,'*') print(v) #結果:******alex ------------------------------------------------------------------------------------------------------------------------------- 27. maketrans(self, *args, **kwargs) #對應關係 + 翻譯 pass """ Return a translation table usable for str.translate(). If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None. Character keys will be then converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to None in the result. """ translate(self, table) return "" """ S.translate(table) -> str Return a copy of the string S in which each character has been mapped through the given translation table. The table must implement lookup/indexing via __getitem__, for instance a dictionary or list, mapping Unicode ordinals to Unicode ordinals, strings, or None. If this operation raises LookupError, the character is left untouched. Characters mapped to None are deleted. """ 例: m = str.maketrans('aeiou','12345') # 對應關係 S = "akpsojfasdufasdlkfj8ausdfakjsdfl;kjer09asdf" v = S.translate(m) print(v) #結果:1kps4jf1sd5f1sdlkfj815sdf1kjsdfl;kj2r091sdf ------------------------------------------------------------------------------------------------------------------------------- 28. partition(self, sep) #分割,保留分割的元素 pass """ S.partition(sep) -> (head, sep, tail) 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. 在S中搜索分離器SEP,並返回它以前的部分、分離器自己和它後面的部分。若是找不到分隔符,則返回S和兩個空字符串。 """ 例: S = "李泉SB劉康SB劉一" v = S.partition('SB') print(v) #結果:('李泉', 'SB', '劉康SB劉一') ------------------------------------------------------------------------------------------------------------------------------- 29. replace(self, old, new, count=None) return "" """ S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. 返回一份用new 取代 S全部的old字符串。若是給定可選參數計數,則只有前 count 個事件被替換。 """ 例: S = "李泉SB劉康SB劉浩SB劉一" v = S.replace('SB','Love') print(v) #結果:李泉Love劉康Love劉浩Love劉一 v = S.replace('SB','Love',1) print(v) #結果:李泉Love劉康SB劉浩SB劉一 ------------------------------------------------------------------------------------------------------------------------------- 30. rfind(self, sub, start=None, end=None) return 0 """ S.rfind(sub[, start[, end]]) -> int 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. 返回 在最大的索引開始 ,字符串 S 中發現子串sub ,這樣的 sub 是包含在 S[start:end]裏。可選參數start和end被解釋爲切片符號。失敗返回-1。 """ 例: S = 'alex' v = S.rfind('e',2,3) print(v) #結果:2 ------------------------------------------------------------------------------------------------------------------------------- 31. rindex(self, sub, start=None, end=None) return 0 """ S.rindex(sub[, start[, end]]) -> int Like S.rfind() but raise ValueError when the substring is not found. 像S.rfind() 可是當沒有找到字符串時 報ValueError """ 例: S = 'alex' v = S.rindex('e',2,3) print(v) #結果:2 ------------------------------------------------------------------------------------------------------------------------------- 32. rpartition(self, sep) pass """ S.rpartition(sep) -> (head, sep, tail) 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. """ 例: S = "李泉SB劉康SB劉一" v = S.rpartition('SB') print(v) #結果:('李泉SB劉康', 'SB', '劉一') ------------------------------------------------------------------------------------------------------------------------------- 33. rsplit(self, sep=None, maxsplit=-1) return [] """ S.rsplit(sep=None, maxsplit=-1) -> list of strings Return a list of the words in 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, any whitespace string is a separator. 返回一個列表,單詞在S中,使用sep做爲分隔符,從字符串的結尾開始並工做到最前面。若是maxsplit是給定的,最多分裂maxsplit次。 若是未指定任何SEP,空格的字符串是一個分離器。 """ 例: S = 'as|ww|ee' v = S.rsplit('|',1) print(v) #結果:['as|ww', 'ee'] ------------------------------------------------------------------------------------------------------------------------------- 34. rstrip(self, chars=None) #去除尾部空格 return "" """ S.rstrip([chars]) -> str Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. 返回一個拷貝的字符串的尾部空格刪除。若是字符是給定的,而不是沒有,刪除字符用chars代替。 """ 例: S = 'ALEX ' v = S.rstrip() print(v) #結果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 35. lstrip(self, chars=None) return "" """ S.lstrip([chars]) -> str Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. 返回一個拷貝的字符串的開頭空格刪除。若是字符是給定的,而不是沒有,刪除字符用chars代替。 """ 例: S = ' ALEX' v = S.lstrip() print(v) #結果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 36. split(self, sep=None, maxsplit=-1) # return [] """ S.split(sep=None, maxsplit=-1) -> list of strings Return a list of the words in 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. 返回一個列表,單詞在S中,用SEP做爲分隔符串。若是maxsplit是給定的,最多分裂maxsplit次。 若是sep未被指定或者是空,任何空白字符是一個分離器而且空白字符在結果中移除。 """ 例: S = 'as|ww|ee' v = S.split('|',1) print(v) #結果:['as', 'ww|ee'] ------------------------------------------------------------------------------------------------------------------------------- 37. splitlines(self, keepends=None) #按回車生產列表 return [] """ S.splitlines([keepends]) -> 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. 返回一個列表,在S中的行,在行邊界處斷開。換行不包含在結果列表中,除非keepends給出併爲Ture。 """ 例: S = """ALEX asd """ v = S.splitlines() print(v) #結果:['ALEX', 'asd'] ------------------------------------------------------------------------------------------------------------------------------- 38. strip(self, chars=None) 移除空白,\n,\t return "" """ S.strip([chars]) -> str 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. 返回一個拷貝的字符串的開頭和結尾的空格去掉。若是字符是給定的,而不是沒有,刪除字符用chars代替。 """ 例: S = ' ALEX ' v = S.strip() print(v) #結果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 39. swapcase(self) # 大小寫轉換 return "" """ S.swapcase() -> str Return a copy of S with uppercase characters converted to lowercase and vice versa. 返回一個s的拷貝,大寫字符轉換爲小寫,反之亦然。 """ 例: S = "Alex" v = S.swapcase() print(v) #結果:aLEX ------------------------------------------------------------------------------------------------------------------------------- 40. title(self) #轉換 標題版 return "" """ S.title() -> str Return a titlecased version of S, i.e. words start with title case characters, all remaining cased characters have lower case. 返回一個titlecased版的S,從單詞開始標題版 字符串,全部剩餘的字符的小寫。 """ 例: S = "i have a dream" v =S.title() print(v) #結果:I Have A Dream ------------------------------------------------------------------------------------------------------------------------------- 41. istitle(self) #是不是標題版 return False """ S.istitle() -> bool Return True if S is a titlecased string and there is at least one character in S, i.e. upper- and titlecase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise. 若是S是一個titlecased字符串而且至少有一個字符在S裏返回True,i.e.大寫 而且 titlecase字符只能跟着未包裝的字符和小寫字符的惟一狀況。不然返回False。 """ 例: S = "I Have A Dream" v = S.istitle() print(v) #結果:Ture ------------------------------------------------------------------------------------------------------------------------------- 42. upper(self) #轉換大寫 return "" """ S.upper() -> str Return a copy of S converted to uppercase. 返回一個S 的大寫副本 """ 例: S = 'Alex' v = S.upper() print(v) #結果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 43. zfill(self, width) #填充0 return "" """ S.zfill(width) -> str Pad a numeric string S with zeros on the left, to fill a field of the specified width. The string S is never truncated. 在左邊填充一個帶零的數字字符串,以填充指定width的字段。字符串從不被截斷。 """ 例: S = "alex" v = S.zfill(10) print(v) #結果:000000alex