Python_string manipulation

 1 name= ' hu\tqihang '                        # \t is the TAB key 

1. Printing related

 

1  print (name.center(50, ' - ' ))               #Print a total of 50 characters, if it is not enough, use - to make up, the name is placed in the middle 
2  print (name.ljust(50, ' - ' ))                #Print a total of 50
 rjust(50, ' - ' ))                #Print a total of 50 characters, if it is not enough - make up, name is placed on the right 4 print  ( name .zfill(50)) #Print                    a total of 50 characters, not enough to fill in the front with 0
 

 

2. Find relevant

 

1  print (name.count( ' a ' ))                   #count the number of occurrences of a 
2  print (name.find( ' qi ' ))                   #find the subscript starting with the leftmost qi 
3  print (name.rfind( ' h ' ))                   #Find the subscript of the rightmost h 4 print (name[name.find( ' qi ' ):])            # find convenient string slices 5 print (name.index( ' qi ' ) )                  #
 
 Same as find, except that an exception will be reported if str is not in the string

 

 

 

3. Judgment related

 

1  print (name.startswith( ' hu ' ))            # Determine whether the string starts with hu 
2  print (name.endswith( ' ng ' ))              # Determine whether the string ends with ng 
3  print ( ' abcABC123 ' .isalnum() )            # Determine whether it is an English character or a number 
4  print ( ' abcABC ' .isalpha())               # Determine whether it is a pure English character 
5  print ( ' 1234 ' .isdigit())                 # Determine whether the string is only composed of numbers
6  print ( ' abc ' .islower()) #Determine                  whether it is all lowercase 
7  print ( ' Abc ' .isupper()) #Judging                  whether it is all uppercase 
8  print ( ' Is Title ' .istitle())             #Judging each Whether the first letter is uppercase 
9  print ( '  ' .isspace())                    # Determine whether it is a space 
10  print ( ' 1000 ' .isdecimal())               # Determine whether the string contains only decimal characters 
11 print ( ' name ' .isidentifier())            # Determine whether it is a valid variable name

 

 

 

4. Change related

 

1  print (name.capitalize()) #capitalize                the first letter 
2  print ( ' is title ' .title()) #capitalize               each first letter of it 
3  print ( ' HUQIHANG ' .lower ())               #capitalize lowercase 
4  print ( ' huqihang ' .upper()) #change               lowercase to uppercase 
5  print ( ' Hu Qihang ' .swapcase()) #uppercase           to lowercase, lowercase to uppercase 
6  print(name.expandtabs(tabsize=30))      # Convert the tab key to 30 spaces 
7  print ( ' \nhuqihang\n ' .strip())           # Remove all carriage returns 
8  print ( ' \nhuqihang ' .lstrip() )            #Remove the carriage return 
9 on the left  print ( ' huqihang\n ' .rstrip()) #Remove            the carriage return 
10 on the right  print ( ' + ' .join([ ' 1 ' , ' 2 ' , ' 3 ']))          #Concatenate the elements in the sequence with the specified characters to generate a new string 
11  print ( ' 1+2+3+4+5+6 ' .split( ' + ' ))         # Press the + Divide into lists, + is the separator, does not appear in the list, name.split() defaults to spaces 
12  print ( ' 1+2+3\n+4+5+6 ' .splitlines()) #divide     by newline list

 

 

 

5. Replace the correlation

 

1 p=str.maketrans( ' abc ' , ' 123 ' )
 2  print (name.translate(p)) #Replace                 abc in name with 123

 

 1 print (name.replace( ' h ' , ' H ' ,1))           #Replace h in name with H, only one 

 

Guess you like

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