There are character formatting methods in most programming languages, which are summarized in the Python language for easy reference:
Way 1:100 Semicolon%
| Format |
Describe |
| %c |
Characters and ASCII codes |
| %s |
String |
| %d |
Signed integer (decimal) |
| %u |
unsigned integer (decimal) |
| %o |
unsigned integer (octal) |
| %x |
unsigned integer (hexadecimal) |
| %x |
unsigned integer (16 uppercase characters) |
| %e |
Floating point number (scientific counting method) |
| %E |
Floating point number (scientific counting method) |
| %f |
Floating point number (decimal symbol) |
| %g |
Floating-point number (%e or%f depending on the size of the value) |
| %G |
Floating point (same as%g) |
| %p |
< Span style= "font-size:14px" > pointer (print memory address in hexadecimal) |
"Example 1" input: > >> name=input ( " name: " >>> age=int (Input ( " age: " >>> sex=input ( " sex: " ) >> > print ( " name:%s\nage:%d\nsex:%s "% (name,age,sex)) output: >>> Name:anna >>> age:18>>> sex:girl
" Example 2" input:>>> Name=input ("Name:")>>> Age=int (Input ("Age :"))>>> Sex=input ("Sex:")>>> info=" "Name:%sage:%dsex:%s" "%(Name,age,sex)Print(Info) Output:>>>Name:anna>>> age:18>>> Sex:girl
"Remark": "(or" ") Effect: 1. Comment on multiple lines of information; 2. The role of the paragraph
Mode two: Format ()
"Example 1" input: >>> info= " tom {0} a {1} " >>> Output=info.format (" is ", " Span style= "COLOR: #800000" >student " ) >>> print >>> Tom is a student
"Example 2" input:>>> info="Tom {Info1} a {Info2}">>> output= Info.format (info2='student', info1='is') Print (output) outputs: is a student
Python string formatting