Python str operation method (detailed description), 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 ).
>>> String = 'python {}, django {}, tornado {}'. format (2.7, 'web', 'tornado ') # correspond to each {} placeholder, enter string> string 'python2. 7. djangoweb, tornadotornado '>>> string = 'python {}, django {}, tornado {}'. format (2.7, 'web') Traceback (most recent call last): File "<pyshell #6>", line 1, in <module> string = 'python {}, django {}, tornado {}'. format (2.7, 'web') IndexError: tuple index out of range >>> 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, you must ensure that the specified position has a value.) >>> string 'python2. 7. djangotornado, tornadoweb '>>> string = 'python {py}, django {dja}, tornado {tor }'. format (tor = 'tornado ', dja = 'web', py = 2.7) # values can be assigned in the form of key-value pairs> string 'python2. 7. djangoweb, tornadotornado '>>>
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) |
The str operation method of the above Python article (detailed description) is all the content that I have shared with you. I hope to give you a reference and support for the help house.