Common exercises to add a string
. 1 # Coding: UTF. 8- 2 . 3 name = " SaDXdxasdf " . 4 # is equivalent to nane = str ( "Lee") , the class calls the __init__ method str . 5 . 6 # type name of the print . 7 Print (type (name) ) . 8 . 9 # all members of its class name print 10 Print (the dir (name)) . 11 12 is # string comprises two methods rather 13 is Print (name. __contains__ ( " EE " )) 14 Print ( " EE " in name) 15 16 # capitalized . 17 Print (name.capitalize ()) 18 is . 19 # all uppercase letters 20 is Print (name.upper ()) 21 is 22 is # all lowercase letters, lower ASCII is only valid for the A ~ Z other languages starting casefold 23 is Print (name.casefold ()) 24 Print (name.lower ()) 25 26 is Print (30 * " * " ) 27 28 # centering the print string, length 25 containing the centered string name 29 str1 = name.center (25, " * " ) 30 Print (str1) 31 is Print (len (str1)) 32 33 is # number of statistical specified string 34 is str2 = " sdfasdfdssddd00000000sdddasdfsafd " 35 Print (str2.count ( " sddd " )) 36 # can specify the starting position and an ending position to find 37 [ Print ( str2.count ( " sddd " , 0, (int) (len (str2) / 2 ))) 38 is 39 # string encoding format in accordance with the specified 40 Str3 = " Chinese " 41 is str4 = str3.encode ( " GBK " ) 42 Print(str4) 43 is # using the decode () decodes, what encoding format compiled code to be decoded in the same format 44 is Print (str4.decode ( " GBK " )) 45 46 is # determines whether the specified character string ( string) end 47 Print (str2.endswith ( " D " )) 48 Print (str2.endswith ( " AFD " ))