Summary of built-in methods for string types
Version: python3.6
When looking at the built-in functions of bulitins, the built-in methods of strings are classified and summarized.
Category method description example finds string.find (sub, start=None, end=None)
Find substrings within the specified range
If found, the smallest index is returned.
If not found, return-1
Mystr = 'hello world' mystr.find ('w') # returns 6mystr.find ('x') # returns-1string.rfind (sub, start=None, end=None)
Find substrings within the specified range
If found, the largest index is returned.
If not found, return-1
Mystr = 'hello world' mystr.find ('l') # returns 2mystr.rfind ('l') # returns 9
String.count (sub, start=None, end=None
)
Find substrings within the specified range
If you find it, the number of substrings will be returned.
If not found, 0 is returned.
Mystr = 'hello world' mystr.count ('w') # returns 3mystr.count ('x') # returns 0string.index (sub, start=None, end=None)
Find substrings within the specified range
If found, the smallest index is returned.
Error that returns ValueError if not found
Mystr = 'hello world' mystr.index ('w') # returns 6mystr.index ('x') # returns ValueError:substring not foundstring.rindex (sub, start=None, end=None)
Find substrings within the specified range
If found, the largest index is returned.
Error that returns ValueError if not found
Mystr = 'hello world' mystr.index ('l') # returns 2mystr.rindex ('l') # returns 9 padding, splicing character string.center (width,fillchar) returns a string of length width. The original string is centered and the filled character can be specified. The filled string is evenly divided between the left and right sides of mystr = 'hello world' mystr.center (18mystr') # returns''hello world' 'string.join (sep) with the string as the delimiter Merge all the elements in sep into a new string mystr = 'hello world' mystr.join ('and') # returns a hello world n hello world dstring.zfill (width) returns a string of length width, the original string is aligned to the right, and the left side is filled with 0mystr =' hello world 'mystr.zfill (17) # returns 00000hello world
String.ljust (width,fillchar)
Returns a string of length width, the original string is aligned to the left, the right side is filled with the specified character mystr = 'hello world' mystr.ljust (18 hello world'') # returns hello world 'string.rjust (width,fillchar) returns a string of length width, the original string is aligned to the right, and the left side is filled with the specified character mystr =' hello world 'mystr.rjust (18 '\') # returns' hello world convert uppercase string.capitalize () converts the first letter to uppercase mystr = 'hello world' mystr.capitalize () # returns Hello worldstring.upper () converts all letters in the string to uppercase mystr = 'hello world' mystr.upper () # returns HELLO WORLDstring.lower () converts all letters in the string to lowercase mystr = 'Hello World' mystr.lower ( ) # return hello worldstring.swapcase () flip case mystr = 'Hello World' mystr.swapcase () # return hELLO wORLDstring.title () convert the first letter of all words to uppercase mystr = 'hello world It\ 's fantastic'mystr.title () # returns Hello World It'S Fantastic delete the space on the left side of the string string.strip () delete the space on the left and right side of the string mystr = 'hello world' mystr.strip () # return hello worldstring.lstrip () delete the space on the left side of the string mystr = 'hello world' mystr.lstrip () # return hello worldstring.rstrip () delete the space on the right side of the string mystr = 'hello world' mystr.rstrip () # return hello world split string string.partition (sep) to the specified character Divide the string into head,sep,tail 3 parts If there is no matching character, return the string and 2 empty strings mystr = 'hello world' mystr.partition ('w') # return ('hello', 'wicked,' orld') mystr.partition (';) # return ('hello world','','') string.rpartition (sep) to the specified character, dividing the string into head,sep,tail 3 parts If there are no matching characters, return 2 empty strings and the string mystr = 'hello world' mystr.rpartition ('w') # return ('hello', 'wicked,' orld') mystr.rpartition (';) # return ('','', 'hello world') string.split (sep=None, maxsplit=-1)
With sep as the delimiter, the slice string mystr = 'hello world' mystr.split (sep='l',maxsplit=2) # returns ['he',','o world'] string.rsplit (sep=None, maxsplit=-1)
With sep as the delimiter, the slice string mystr = 'hello world' mystr.rsplit (sep='l',maxsplit=2) # returns ['hel',' o wor', d'] string.splitlines (keepends=None)
) separated by lines ('\ r\ nlines,\ n'), returns a list containing lines as elements. If the parameter keepends is False and does not contain newline characters, if it is True, the newline character mystr = 'hello world,\ n it\' s fantastic'mystr.splitlines () # returns ['hello world,', "it's fantastic"] to replace string.replace (old, new, count=None)
) string substitution mystr = 'hello world' mystr.replace (' world','Beijing') # returns at least one string in the hello Beijingis series string.isalpha () str and all characters are letters, returns True, otherwise returns Flasemystr1 = 'hello, world' mystr2 =' hello'mystr1.isalpha () # returns Falsemystr2.isalpha () # returns at least one string in Truestring.isalnum () str and all characters are letters or numbers, returns True Otherwise, Flasemystr1 = 'hello, world! 'mystr2 =' hello123'mystr1.isalnum () # returns Falsemystr2.isalnum () # returns True if Truestring.isdemical () str contains only decimal digits, otherwise returns Falsemystr1 = '1091A' mystr2 = '1091'mystr1.isdecimal () # returns Falsemystr2.isdecimal () # returns Truestring.isdigit () str only contains numbers, returns True Otherwise, return Falsemystr1 = '1091A' mystr2 = '1091'mystr1.isdigit () # return Falsemystr2.isdigit () # return Truestring.isnumeric () str contains only numeric characters, return True, otherwise return Falsemystr1 =' 1091A 'mystr2 =' 1091'mystr1.isnumeric () # return Falsemystr2.isnumeric () # return Truestring.istitle () str is themed, return True Otherwise, return Falsemystr1 = 'hello world' mystr2 = 'Hello World'mystr1.istitle () # return Falsemystr2.istitle () # return Truestring.islower
() return True if all characters are lowercase, otherwise return Falsemystr1 = 'Hello world' mystr2 =' hello world'mystr1.islower () # return Falsemystr2.islower () # return Truestring.isupper ()
Return True if all characters are uppercase, otherwise return Falsemystr1 = 'Hello world' mystr2 =' hello world'mystr1.isupper () # return False (mystr2.swapcase ()) .isupper () # return True