27 common methods of python strings

Given the string mystr = 'hello world itcast and itcastcpp' , the following are common operations:
<1>find
Check whether str is included in mystr, if it is, return the starting index value, otherwise return -1
mystr.find(str, start=0, end=len(mystr))
 
<2>index
The same as the find() method, except that an exception will be reported if str is not in mystr.
mystr.index(str, start=0, end=len(mystr))
 
<3>count
Returns the number of times str appears in mystr between start and end
mystr.count(str, start=0, end=len(mystr))
 
<4>replace
Replace str1 in mystr with str2, if count is specified, the replacement will not exceed count times.
mystr.replace(str1, str2, mystr.count(str1))
 
<5>split
Slice mystr with str as the delimiter, if maxsplit has a specified value, then only maxsplit substrings are separated
mystr.split(str=" ", 2)
 
<6>capitalize
Capitalize the first character of a string
mystr.capitalize()
 
<7>title
Capitalize the first letter of each word in a string
>>> a = "hello itcast"
>>> a.title()
'Hello Itcast'
 
<8>startswith
Check if the string starts with hello, return True if it is, otherwise return False
mystr.startswith(hello)
 
<9>endswith
Checks if the string ends with obj, returns True if so, False otherwise.
mystr.endswith(obj)
 
<10>lower
converts all uppercase characters in mystr to lowercase
mystr.lower()
 
<11>upper
Convert lowercase letters in mystr to uppercase
mystr.upper()
 
<12> bright
Returns a new string of length width that is left justified and padded with spaces
mystr.ljust (width)
 
<13>rjust
Returns a new string of length width right-aligned from the original string and padded with spaces
mystr.rjust(width)
 
<14>center
Returns a new string of length width centered on the original string and padded with spaces
mystr.center(width)
 
<15>lstrip
remove whitespace characters to the left of mystr
mystr.lstrip()
 
<16>rstrip
remove whitespace at the end of mystr string
mystr.rstrip()
 
<17>strip
Remove whitespace characters from both ends of mystr string
>>> a = "\n\t itcast \t\n"
>>> a.strip()
'itcast'
 
<18>rfind
Similar to the find() function, but searches from the right.
mystr.rfind (str, start = 0, end = len (mystr))
 
<19>rindex
Similar to index(), but starts from the right.
mystr.rindex (str, start = 0, end = len (mystr))
 
<20>partition
Divide mystr into three parts by str, before str, after str and after str
mystr.partition(str)
 
<21>rpartition
Similar to the partition() function, but starts from the right.
mystr.rpartition (str)
 
<22>splitlines
Separated by lines, returns a list containing each line as an element
mystr.splitlines()
 
<23>isalpha
Returns True if all characters in mystr are letters, otherwise returns False
mystr.isalpha()
 
<24>isdigit
Returns True if mystr contains only numbers and False otherwise.
mystr.isdigit()
 
<25>isalnum
Returns True if all characters of mystr are letters or numbers, otherwise returns False
mystr.isalnum ()
 
<26>isspace
Returns True if mystr contains only spaces, otherwise returns False.
mystr.isspace()
 
<27>join
Insert str after each element in mystr to construct a new string
mystr.join(str)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325379937&siteId=291194637