Python str operation, pythonstr
1. str. format (): format the string using the "{}" Placeholder (the index number format and key-value pairs in the placeholder can be used together ).
1 >>> string = 'python {}, django {}, tornado {}'. format (2.7, 'web', 'tornado ') # correspond to each {} placeholder, enter 2> string 3 'python2. 7. djangoweb, tornadotornado '4 >>> string = 'python {}, django {}, tornado {}'. format (2.7, 'web') 5 Traceback (most recent call last): 6 File "<pyshell #6>", line 1, in <module> 7 string = 'python {}, django {}, tornado {}'. format (2.7, 'web') 8 IndexError: tuple index out of range 9 >>> string = 'python {0}, django {2}, tornado {1 }'. format (2.7, 'web', 'tornado ') # You can also specify the value to be entered (starting from 0, the subsequent values may not always be used, however, make sure that the specified position has a value.) 10 >>> string11 'python2. 7. djangotornado, tornadoweb '12 >>> string = 'python {py}, django {dja}, tornado {tor }'. format (tor = 'tornado ', dja = 'web', py = 2.7) # You can assign values in the form of key-value pairs 13 >>> string14' python2. 7. djangoweb, tornadotornado '15 >>>
2. Use "%" to format the string.
Format a symbol table
| % C |
Convert to single character |
| % R |
Convert to a string expressed in repr () |
| % S |
Converted to a string expressed by str () |
| % D or % I |
Convert to a signed decimal integer |
| % U |
Convert to an unsigned decimal integer |
| % O |
Convert to an unsigned octal integer |
| % X |
Converts the string to an unsigned hexadecimal integer. The hexadecimal letter is in lowercase. |
| % X |
Converts the string to an unsigned hexadecimal integer. The hexadecimal letter is in uppercase. |
| % E |
Converts it to a floating point number expressed in scientific notation, where e is expressed in lowercase. |
| % E |
Converts it to a floating point number expressed in scientific notation, where E is expressed in uppercase. |
| % F or # F |
Convert to floating point |
| % G |
Python automatically converts the value to % e or % f based on the number size. |
| % G |
Python automatically converts the value to % E or % F based on the number size. |
| % |
Output "%" |
Auxiliary formatting symbol table
| * |
Defines the width or decimal point Precision |
| - |
Left aligned |
| + |
Returns the positive value symbol "+" to a positive number" |
| <Sp> |
When the number size is less than m. n, fill in space |
| # |
Display 0 before the octal digit, and 0x or 0X before the hexadecimal digit |
| 0 |
When the number size is less than m. n, use 0 to fill |
| M. n |
M is the minimum total width displayed, and n is the number of digits after the decimal point (if available) |