1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #Auther:summer4 5 #string Manipulation6Name ="Summer Du"7 Print(Name.capitalize ())#capitalize the initial letter8 Print(Name.casefold ())#uppercase letters All lowercase9 Print(Name.center (50,"="))#Output ====================summer du=====================Ten Print(Name.count ("m"))#Count M occurrences One Print(Name.encode ())#encode a string into bytes format A Print(Name.endswith ("m"))#determines whether a string ends in M - Print(Name.find ("D"))#Find D, find returns its index value, cannot find return-1, return it to find the first index value - theName ="Sum\tmer Du" - Print(Name.expandtabs (10))#Output sum mer Du - - #format +msg ="My name is {},and-is {}" - Print(Msg.format ("Summer"," -")) +msg ="My name is {1},and-is {0}" A Print(Msg.format ("Summer"," A")) atmsg ="My name is {name},and ' {age}" - Print(Msg.format (name ="Summer", age =" A")) - Print(Msg.format (age =" A", name ="Summer")) - - #Format_map - Print(Msg.format_map ({' Age': 22,'name':"Summer"})) inmsg ="My name is {name},and ' {age}" - Print(Msg.index ("m")) to + Print("9Aa". Isalnum ()) - Print("9". IsDigit ())#whether integer the Print(Name.isnumeric ()) * Print(Name.isprintable ()) $ Print(Name.isspace ())Panax Notoginseng Print(Name.istitle ()) - Print(Name.isupper ()) theName ="Summer Du" + Print(Name.join ("Lee")) A Print("|". Join (["Lee","Summer"]))#put it in the list. the + #the Maketrans Python Maketrans () method is used to create a conversion table for character mappings. - #for the simplest invocation method that accepts two parameters, the first argument is a string that represents the character that needs to be converted, $ #The second parameter is also the target of the string representation transformation. $Intab ="Aeiou" -Outtab ="12345" -Trantab =Str.maketrans (Intab,outtab) the Print(Trantab) - Wuyi #Partition the Print(Msg.partition (" is")) - WuR ="My name is Summer,and" - Print(R.replace ("m","N", 2)) About $ Print(Name.swapcase ())#uppercase and lowercase swaps - - Print(Msg.zfill (40)) - Print(Msg.ljust (40,"-")) A Print(Msg.rjust (50,"=")) + Print(Msg.center (50,"=")) the -b ="Ddefs;ajfdal;_haho (∩_∩) o haha ~" $c ="dd_ haha" the Print(B.isidentifier ())##检测一段字符串可否被当作标志符, that is, whether to conform to variable naming rules the Print(C.isidentifier ())##检测一段字符串可否被当作标志符, that is, whether to conform to variable naming rules
Python string manipulation