1, strip, Rstrip, Lstrip
1 name=' a b \ n'2print# default minus the left space and line break 3print(Name.rstrip ())# default minus the right space and line break 4Print (Name.lstrip ())# default to remove both spaces and line breaks
Return
A b C
2. String Case Conversion:
1 name= " lily lilei " 2 print (Name.upper ()) # turn all letters into uppercase 3 print (Name.lower ()) # Turn all letters into lowercase 4 print (name.capitalize ()) # capitalize the first letter and turn the rest into lowercase 5 print (Name.title ()) # returns
LILY lileilily lileilily lileilily Lilei
3, the string case to judge:
1Name="Lily Lilei"2 Print(Name.islower ())#are they all lowercase?3 Print(Name.isupper ())#are they all uppercase?4 Print(Name.istitle ())#whether to capitalize the first letter of each word5 Print(Name.isalnum ())#Whether it contains numbers or letters6 Print(Name.isalpha ())#are they all English letters?
Return
truefalsefalsefalse# Because there is a space, so false
false# because there is a space, so false
4. Replace (old,new) function: Replace
1 name="Lily Lilei"2print(name.replace ('Lily ','zhaosi'))
Return
Zhaosi Lilei
5. Find, Index
Function:find (), the index () method detects whether the string contains substrings of STR, and if the beg (start) and end (end) ranges are specified, checks whether the containing substring returns the starting index value if it is contained within the specified range.
Difference: Find, if the found string does not exist, returns -1;INDEX, if the found string does not exist, error!
Syntax:str. Find(str, beg=0, End=len(string))
< Span class= "PLN" >< Span class= "PLN" >< Span class= "pun" > str. Index(str, beg=0, End=len(string))
Parameters
-
STR--Specifies the retrieved string
-
Beg--Start index, default is 0.
-
End--ends the index, which defaults to the length of the string.
find
1 name1='lily Zhaosi Wangwu'2 name2='zhaosi' 3print(Name1.find (name2)) # starting with subscript 0, look for the first substring that appears in a string 4 Print(Name1.find (name2,5))# starting with subscript 5, find the first substring that appears in a string 5Print (Name1.find (name2,6))# starting with subscript 6, find the first occurrence of a substring in a string, not found, return-1
Return
55-1
index
1 name1= " lily zhaosi Wangwu " 2 name2= " Zhaosi " 3 print (Name1.index (name2)) # Starting with subscript 0, find the first substring that appears in the string 4 print (Name1.index (name2,5)) # starting with subscript 5, find the substring that appears first in the string 5 print (Name1.index (name2,6)) # starting with subscript 6, find the first substring that appears in the string, if not present, throw an exception
returns
Traceback (most recent):print(Name1.index (name2,6))# starting with subscript 6, Finds the first substring that appears in a string, throws an exception if it does not found
6. The count () method is used to count the occurrences of a character in a string. The optional parameters are the start and end positions of the string search.
syntax: str. Count(Sub, start= 0,end=len(string))
Parameters:
-
Sub--The substring of the search
-
Start--the location where the string starts searching. The default is the first character, and the first character index value is 0.
-
End--the location of the ending search in the string. The index of the first character in a character is 0. The default is the last position of the string.
1 name1='lily Zhaosi Wangwu'2 sub='w' 3 Print (Name1.count (sub,5,16))
Return
1
7. Split () slices the string by specifying a delimiter, and if the parameter num has a specified value, it separates only the NUM substring, returning the segmented list of strings.
Syntax:str. Split(str="", num=string. Count(str)).
Parameters:
- STR--the delimiter, which defaults to all null characters, including spaces, line breaks (\ n), tabs (\ t), and so on.
- Num--the number of splits.
1 str = " line1-abcdef \nline2-abc \nline4-abcd " 2 print Str.split (); 3 print str.split ( " ", 1);
Return
['line1-abcdef'line2-abc'line4-abcd ' ['line1-abcdef'\nline2-abc \nline4-abcd' ]
Python Basics-string