Common string operations notes:
<1>find
Str is included in the detection mystr, if the index value of the return, otherwise -1
mystr.find(str, start=0, end=len(mystr))
str-- specified search string
beg-- starting index, defaults to 0
end-- end index, string length defaults
Returns: If you include the substring returns the index started, otherwise -1
<2>index
Like find () method, except that if not str mystr will report an exception.
mystr.index(str, start=0, end=len(mystr))
<3>count
Returns the number of times between the start and end str appear in mystr inside
mystr.count(str, start=0, end=len(mystr))
<4>replace
Replace mystr in str1 into str2, if the count is specified, the replacement does not exceed count times.
mystr.replace(str1, str2, mystr.count(str1))
<5>split
Takes str separator slice myStr, if maxsplit value is specified, only the partition maxsplit substrings
mystr.split(str=" ", 2)
<6>capitalize
The first character uppercase
mystr.capitalize()
<7>title
Each word capitalized string
<8>startswith
Check if the string begins with obj, is True is returned, otherwise it returns False
mystr.startswith(obj)
<9>endswith
Check whether the string obj end, a return True, otherwise False.
mystr.endswith(obj)
<10>lower
Mystr convert all uppercase characters to lowercase
mystr.lower()
<11>upper
Conversion mystr in lowercase letters to uppercase
mystr.upper()
<12> light
Returns a string of the original left-aligned and padded with spaces to the width of the new string length
mystr.ljust (width)
<13>rjust
Returns a string of the original right-aligned and padded with spaces to the width of the new string length
mystr.rjust(width)
<14>center
Returns a string of the original center, and padded with spaces to the width of the new string length
mystr.center(width)
<15>lstrip
Delete the left mystr whitespace
mystr.lstrip()
<16>rstrip
Delete the end of the string of blank characters mystr
mystr.rstrip()
<17>strip
Whitespace characters at both ends of the string deleted mystr
<18>rfind
Similar to the find () function, but start looking from the right.
mystr.rfind (p, start = 0, end = len (mystr))
<19>rindex
Similar to the index (), but from the right.
mystr.rindex (p, start = 0, end = len (mystr))
<20>partition
In the mystr str divided into three parts, str front, and rear str str
mystr.partition(str)
<21>rpartition
Similar to the partition () function, but from the right.
mystr.rpartition(str)
<22>splitlines
Spaced in rows, each row containing returns a list as an element
mystr.splitlines()
<23>isalpha
If all the characters are letters mystr returns True, otherwise False
mystr.isalpha()
<24>isdigit
If mystr contain only numbers returns True otherwise False.
mystr.isdigit()
<25>isalnum
If mystr all characters are letters or numbers returns True, otherwise False
mystr.isalnum ()
<26>isspace
If mystr contains only spaces, returns True, otherwise False.
mystr.isspace()
<27>join
str mystr inserted behind each character, a new character string constructed
mystr.join(str)
exercise:
Given a string aStr, returns or spaces '\ t' reciprocal second divided substring testStr = "haha nihao a \ t heihei \ t woshi nidde \ t hao \ npengyou"
Error handling:
testStr="haha nihao a \t heihei \t woshi nidde \t hao \npengyou" c=testStr.split(r"\t\n") print(c)
the reason:
Syntax split () method is as follows
str.split(str="", num=string.count(str)).
Wherein delimiter str default for all null characters, including spaces, linefeed (\ n-), tab (\ t) and the like.
So here, just .split () can
testStr="haha nihao a \t heihei \t woshi nidde \t hao \npengyou" c=testStr.split() print(c[-2])