----------------------------------------------
Other common string methods in real-world applications
>>>line = "The Knights who say Ni!\n"
>>>line.restrip (); Clear the end of the space
The Knights who say ni!
>>>line.upper () Case Conversion
The KNIGHTS who SAY ni!\n
The >>>line.isalpha () Isalpha () method detects whether a string consists of only letters.
False
>>>line.endswith (' ni!\n ') detects if the string ends with NI
True
>>>line.startswitch (' the ') detects if the string starts with the
True
>>>line.find (' ni ')!=-1 character detection
True
>>> ' ni ' in line
True
>>> sub = ' ni\n '
Line.endswitch (sub)
True
>>>line[-line (sub):]==sub
True
--------------------------------------------
The string format code has already been mentioned before, and we don't write an example.
String Formatting code
s string
R S but using REPR is not str
C character
D Decimal
I integer
U unsigned integer
o octal integer
X hex This man you high number
E floating-point index
F Floating Point Decimal
G floating point E or F
constant
Dictionary-based string formatting
String formatting also allows the left translation target to reference the keys in the right dictionary to extract the corresponding values
>>> '% (n) d% (x) s '%{"n": 1, "x": "Spam"}
' 1 spam '
>>>food = ' spam '
>>>age = 40
>>var ()
{' food ': ' Spam ', ' age ': 40}
>>> "% (age) d percent (food) S"%var ()
' Spam '
left-Justified and right-aligned string formatting
>>> ' {0:10} = {1:10} '. Format (' spam ', 123.4567)
' Spam = 123.4567 '
>>> ' {0:>10} = {1:<10} '. Format (' spam ', 123.4567)
' Spam = 123.4567 '
>>> ' {0[' platform ']:>10} = {1[item]:<10} '. Format ({' Platform ': ' Spam '},dict (item = 123.4567));
' Spam = 123.4567 '
The format method can also support hexadecimal octal and binary
>>> ' {0:x},{1:o},{2:b} '. Format (255,255,255)
' ff,277,11111111 '
>>> ' {0:.2f} '. Format (1/3.0)
' 0.33 '
>>> '%.2f '% (1/3.0)
' 0.33 '
Python learning string (bottom)