String 02
Remove white space
>>> " Remove left margin".lstrip() 'Remove left margin' >>> "Remove white space on the right ".rstrip() 'Remove white space on the right' >>> " Don't leave left and right blank ".strip() 'Don't leave left and right blank'
Remove char characters
>>> "www.google.com".lstrip("wcom") '.google.com' >>> "www.google.com".lstrip(".wcom") 'google.com' >>> "www.google.com".rstrip(".wcom") 'www.google' >>> "www.google.com".strip(".wcom") 'google' "strip Remove only the characters in parentheses"
Delete specific characters at the beginning and end
>>> "www.google.com".removeprefix("www.") 'google.com' >>> "www.google.com".removesuffix(".com") 'www.google'
Split & splice
Split text before and after a specific flag
>>> "www.google.com".partition(".") ('www', '.', 'google.com') >>> 'www.google.com/python'.rpartition('/') ('www.google.com', '/', 'python') >>>
Split text
>>> "At the beginning of human life, nature is good, nature is similar, and habits are far away".split() ['At the beginning of human life, nature is good, nature is similar, and habits are far away'] >>> "At the beginning of human life, nature is good, nature is similar, and habits are far away".split(",") ['The beginning of man', 'the theory of Mencius that men are born good', 'Sexual similarity', 'their habits become widely different'] >>> "At the beginning of human life, nature is good, nature is similar, and habits are far away".split(",",1) ['The beginning of man', 'The nature is good, the nature is similar, and the habit is far away'] >>> "At the beginning of human life, nature is good, nature is similar, and habits are far away".rsplit(",",1) ['At the beginning of man, nature is good and similar', 'their habits become widely different']
Split text by line
>>> "At the beginning of man,/n Nature is good,/n Similar in sex,/n their habits become widely different".splitlines() ['At the beginning of man,/n Nature is good,/n Similar in sex,/n their habits become widely different'] >>> "At the beginning of man,\n Nature is good,\n Similar in sex,\n their habits become widely different".splitlines() ['At the beginning of man,', 'Nature is good,', 'Similar in sex,', 'their habits become widely different'] >>> "At the beginning of man,\r Nature is good,\r Similar in sex,\r their habits become widely different".splitlines() ['At the beginning of man,', 'Nature is good,', 'Similar in sex,', 'their habits become widely different']
Splicing
>>> ".".join(['www','google','com']) 'www.google.com' >>> '+'.join(('i','Love','U')) 'i+Love+U'
Two methods to copy the same text
>>> s='I Love U!' >>> s+=s >>> s 'I Love U!I Love U!' >>> "".join(('I Love U!',"I Love U!")) 'I Love U!I Love U!' "Adding is not as efficient as adding join Height of function"
Splicing
>>> year=1997 >>>> "My date of birth is{}".format(year) 'My date of birth is 1997' >>> "1+1={},2*2={},3*3*3={}".format(1+1,2*2,3*3*3) '1+1=2,2*2=4,3*3*3=27' >>> "{}notice{}Very excited".format("vookii","cute girl") 'vookii I'm very excited to see Miss' >>> "{1}notice{0}Very excited".format("vookii","cute girl") 'Little sister saw vookii Very excited' >>> "{0}{0}{1}{1}".format("yes","wrong") 'right and wrong' 'I'm slag Hui. Martial brother will cut me' >>> "I am{name},It's a brother{act}".format(name="Zha Zha Hui",act="Cut me") 'I'm slag Hui. If I'm a brother, cut me' "How to output{}" >>> "{},{},{}".format('1','{}','3') '1,{},3' >>> "{},{{}},{}".format('1','3') '1,{},3' >>>
value | meaning |
---|---|
'<' | Forces the string to align left within free space (default) |
'>' | Forces the string to align right in free space |
'=' | Force padding to prevent the position after the symbol (if any) but before the number (this applies to printing strings as "+ 000000120") |
'^' | Forces the string to be centered within free space |
>>> "{:^}".format(123) '123' >>> "{:^10}".format(123) ' 123 ' >>> "{1:>10}{0:<10}".format(520,250) ' 250520 ' >>> "{left:>10}{right:<15}".format(right=520,left=250) ' 250520 ' >>> "{:010}".format(521) '0000000521' >>> "{:010}".format(-521) '-000000521' >>> "{1:%>10}{0:%<10}".format(520,250) '%%%%%%%250520%%%%%%%' >>> "{:0=10}".format(520) '0000000520'
format string
value | meaning |
---|---|
'+' | Positive numbers are preceded by a positive sign (+) and negative numbers by a negative sign (-) |
'-' | Only negative numbers are preceded by (-), the default behavior |
Space | Positive numbers are preceded by a space, and negative numbers are preceded by a minus sign (-) |
>>> "Add sign" >>> "{:+} {:-}".format(520,-250) '+520 -250' >>>> "Set thousands" 'Set thousands' >>> "{:,}".format(123456) >>>> "{:_}".format(123456) '123_456'
1. For floating-point numbers with [type] set to 'f' and 'f', it is to limit the number of digits displayed after the decimal point;
2. For floating-point numbers whose [type] is set to 'g' or 'g', it limits the total number of digits displayed before and after the decimal point;
3. For non numeric types, the limit is the size of the maximum field;
4. For integer type, the [. precision] option is not allowed.
>>> "{:.2f}".format(3.1415) '3.14' >>> "{:.2g}".format(3.1415) '3.1'
>>> "{:.6}".format("I Love Python") 'I Love' "For non numeric types, the limit is the size of the maximum field" >>> "{:.2}".format(520) Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> "{:.2}".format(520) ValueError: Precision not allowed in integer format specifier "For integer types, use is not allowed[.precision]option"
value | meaning |
---|---|
'b' | Output parameters in binary form |
'c' | Output parameters as Unicode characters |
'd' | Output parameters in decimal form |
'o' | Output parameters in octal form |
'x' or 'x' | Output parameters in hexadecimal form |
'n' | Similar to'd ', except that it is inserted in the appropriate position using the delimiter set by the current locale |
None | Like'd ' |
>>> "{:b}".format(80) '1010000' >>> "{:c}".format(80) 'P' >>> "{:d}".format(80) '80' >>> "{:o}".format(80) '120' >>> "{:x}".format(80) '50' "Front plus#Prompt for character base“ >>> "{:#b}".format(80) '0b1010000' >>> "{:#d}".format(80) '80' >>> "{:#o}".format(80) '0o120' >>> "{:#x}".format(80) '0x50'
value | meaning |
---|---|
'e' or 'e' | Output the parameters in the form of scientific counting method (indicate the index with the letter 'e', and the default accuracy is 6) |
'f' or 'f' | Output the parameters in the form of fixed-point representation ('not a number 'is marked with' nan ', infinity is marked with' inf ', and the default precision is 6) |
'g' or 'g' | In general format, decimals are output in the form of 'f', and large numbers are output in the form of 'e' |
'n' | Similar to 'g', the difference is that it will be inserted in the appropriate position using the separator set by the current locale |
'%' | Output as a percentage (multiply the number by 100 and display as a fixed-point representation followed by a percentage sign) |
None | Similar to 'g', the difference is that when using fixed-point representation, at least one digit will be displayed after the decimal point, and the default precision is consistent with the precision required for the given value |
>>> "{:e}".format(3.1415) '3.141500e+00' >>> "{:f}".format(3.1415) '3.141500' >>> "{:g}".format(123456789) '1.23457e+08' >>> "{:g}".format(1234.56789) '1234.57' >>> "{:%}".format(0.85) '85.000000%' >>> "{:.2%}".format(0.85) '85.00%' >>> "{:.{gjz}%}".format(0.85,gjz=2) '85.00%'
f-string
>>> year=1997 >>> f"My year of birth is{year}" 'My year of birth is 1997' >>> f"1+1={1+1},2*2={2*2},3*3*3={3*3*3}" '1+1=2,2*2=4,3*3*3=27' >>> f"{-520:010}" '-000000520' >>> f"{123456789:,}" '123,456,789' >>> f"{3.1415:.2f}" '3.14'