Python Basic Notes Series Eight: String Operations and Related Functions

  This series of tutorials is for personal study notes. If you want to browse, you may need other programming language basics (such as C language), why? Because I am bad at writing, only I can understand it! !

  • String operations
    1. String addition and multiplication
      
    Python not only supports string addition, but also string multiplication, which is quite convenient to use. Addition is the concatenation of two strings, and multiplication is the addition of strings multiple times.
    example:
    1 str8 = ' hello ' 
    2 str9 = ' world! ' 
    3  # String concatenation 
    4  print str8+ str9 
     5  # Realize the output of 10 horizontal lines 
    6  print  " - " *10
     7  # Output 3 hello 
    8  print str8*3

    output:

    1 hello world!
    2 ----------
    3 hello hello hello 

    2. Member operators of strings
      
    1) in and not in operations: the in operation is used to determine whether a character or substring is in a string, and the not in operator is the opposite of the in operation to determine a character or substring is not in the string.
    example:

    1  #If the string contains the given string, return True 
    2 str8 = ' hello ' 
    3  print  ' he '  in str8
     4  #If the string does not contain the given string, return True 
    5  print  ' hc '  not  in str8

    output:

    1 True
    2 True

      2) Formatting of strings (support for placeholders)
    Example:

    1  #Formatting of strings (supporting placeholders) 
    2 dic = ( ' yyc ' ,50 )
     3  print  ' my name is %s,weight is %d kg. ' %dic

    output:

    my name is yyc,weight is 50 kg.

     

  • String Functions
    Strings have many functions, and the following examples are the most commonly used ones to illustrate.
    example:

    1 mystr = ' hello world start leaning and hello and world! ' 
    2  print mystr.find( ' and ' ) #find the first occurrence of 'and' in the string 
    3  print mystr.find( ' and ' ,27) #Find 4 from the 27th position 
    print mystr.find( ' and ' ,0,len(mystr)) #From the first character to the end of the string to find the position where the first 'and' appears, you can default start ,end is the starting and ending parameter 5 # -----mystr.rfind() Find 6 from the right print mystr.index( ' and ' )   # 
     
     Similar to find, but if the parameter in index does not exist in mystr, an exception will be thrown. 
    7  print mystr.count( " and " ) #Count the number of occurrences of 'and', which can also have 3 parameters like find 
    8 mystr1 = mystr.encode(encoding= " utf-8 " )    #encode 9 according to the specified encoding 
    print type(mystr1)    
     10 mystr2 = mystr1.decode(encoding= " utf-8 " ,errors= " strict " )   #according to the specified encoding Decoding, 11 # The errors parameter is strict, if the encoding error will throw a ValueError exception, unless errors specify ignore or replace 12 print mystr2
     13 
     
      print type(mystr2)
     14  print mystr.replace( ' and ' , ' or ' ) #string replacement function, returns the replaced string, but mystr itself has not changed, unless mystr=mystr.replace('and', 'or') 
    15  print mystr
     16  print mystr.replace( ' and ' , ' or ' ,1) #replace only once 
    17  print mystr.split( '  ' ) #divide by spaces and put them in a list 
    18  print mystr .split( '  ' ,3) #Divide into 4 substrings according to spaces 
    19  # ****************In addition, there are many judgment functions for strings (check the documentation, there are too many)******** ****************

    output:

     1 26
     2 36
     3 26
     4 26
     5 2
     6 <type 'str'>
     7 hello world start leaning and hello and world!
     8 <type 'unicode'>
     9 hello world start leaning or hello or world!
    10 hello world start leaning and hello and world!
    11 hello world start leaning or hello and world!
    12 ['hello', 'world', 'start', 'leaning', 'and', 'hello', 'and', 'world!']
    13 ['hello', 'world', 'start', 'leaning and hello and world!']

     


      

Guess you like

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