String formatting:
A string format method is mentioned earlier to review:
Print (' Hisname was%s'Jeff ' is%s ' Jeff
In fact, there are more than one format method:
Print ('{0}, {1}'. Format ('Alex',' 33
The following values are sorted in order. But it doesn't seem so intuitive to improve
Print ('{name},{age}'. Format (name='Jeff', age='23 ') Jeff,23
This is not very intuitive, the corresponding relationship at a glance
Print ('%-10s%-10s%-10s' % ('name','age' ,'job')) name age Job
%-10s Knot string 10 Characters of space, in the alignment of the output is very useful oh.
Here's a look at the string method:
' Jeff '>>> name. __contains__ ('ff') # ' FF ' is contained in name True
>>> name.capitalize ()#Capitalize first letter'Jeff'>>> Name.center (20)#Center within 20 character spaces'Jeff'>>> Name.center (20,'*')#You can customize the center'********jeff********'>>> Name.count ('F')#Count2>>> Name.count ('F', 0,2)#count in slices0>>> Name.count ('F', 0,3)1>>> Name.endswith ('F')#judging not to end with FTrue>>> Name.endswith ('e', 0,2)#Judging in slicesTrue>>> name ='J\teff'>>> Name.expandtabs ()#Displays the escape character in the string, \ t is the tab'J Eff'>>> Name.index ('F')#return index value3>>> Name.find ('ex')#return index value, not found return-1-1>>> Name.find ('FF')#R Returns an index, the first matching3>>> Name.index ('x')#An exception is thrown when the index method is not foundTraceback (most recent): File"<stdin>", Line 1,inch<module>valueerror:substring notfound>>>
>>> name ='Jeff'>>> Name.isdigit ()#whether it is a numeric number, note that negative numbers also return false-oh, positive integers and 0 return trueFalse>>> Name.isalnum ()#are they all letters or numbers?True>>> Name.isalpha ()#are they all letters?True>>> Name.isdecimal ()#whether it is a decimal decimalFalse>>> Name.isidentifier ()#Judging whether a string is legal, is actually judging whether the variable is legitimateTrue>>> Name.islower ()#are they all lowercase?True>>> Name.isupper ()#are they all uppercase?False>>> Name.isnumeric ()#whether it is a numberFalse>>> Name.isspace ()#whether it is a spaceFalse>>> Name.istitle ()#whether it is a titleFalse
Join () method
>>> li = ['a','b','C' ,'d']'_'. Join (LI) 'a_b_c_d'
Strip () method: Remove the left and right side of the space, newline characters will be removed oh.
>>>'name'. Strip ()'name'>>>'name'. Strip ()'name'>>>'name\n'. Strip ()'name'>>>'\nname\n'. Strip ()'name'
Lstrip (): Remove the Left
Rstrip (): Remove the right
Look at the following.
>>> intab = " aeiou " >>> Outtab = " 12345 " >>> from string import Maketrans >>> trantab = Maketrans (inta B,outtab) # python3 here will throw an exception do not know why >> > str = " this is string example.....wow!! " >>> print Str.translate (trantab) th3s 3s str3ng 2x1mpl2.....w4w!!
print str.translate (trantab,'xm') # replaced by mapping, and moved out of X and Mth3s 3s str3ng 21pl2.....w4w!!
Go ahead, look at a segmentation method
' JACKISPPP '>>> name.partition (' is') ('Jack') ' is''PPP')
Peplace () Replace
>>> name = " jack is teacher " Span style= "COLOR: #800000" > " >>> name.replace (" a " , " x " ) #将所有a替换成x Span style= "COLOR: #800000" > " jxck is Texcher "
>>> name.replace ('C','T', 2) #替换两个 ' Jatk is TeaTher '
Split ()
Splitlines ()
StartsWith ()
Swapcase () Case Conversion
Title () converted to caption
Upper () turn into uppercase
Python (day2)-string (2)