In the process of writing a program, it is often necessary to format the output, each time with a check. Just sort it out here for indexing.
Format operator (%)
"%" is a Python-style string formatting operator, very similar to the C language of the printf () function of the string formatting (C language is also used in%).
Here's a look at the string formatting in Python conforms to:
|
Formatting symbols |
Description |
|
%c |
Convert to character (ASCII value, or string of one length) |
|
%r |
Prioritize string conversions with the repr () function |
|
%s |
Prioritize string conversions with the STR () function |
|
%d/%i |
Turn into a signed decimal number |
|
%u |
Turn into unsigned decimal numbers |
|
%o |
Turn unsigned octal number |
|
%x/%x |
Turns to unsigned hexadecimal number (x/x represents the case of the converted hexadecimal character) |
|
%e/%e |
Turn into scientific notation (e/e control output e/e) |
|
%f/%f |
Convert to floating point (fractional part naturally truncated) |
|
%g/%g |
Shorthand for%e and%f/%e and%f |
|
%% |
Output% (the formatted string includes a percent semicolon, so you must use a percentile) |
The formatting that is listed here is fairly simple, and the only thing you want to emphasize is the difference between "%s" and "%r".
To see a simple code:
" hello\twill\n " Print " %s " %stringprint"%r" %string
The output of the code is:
In fact, the difference here is the difference between the two built-in functions of STR () and repr ():
- STR () The resulting string is user-oriented and has good readability
- Repr () The resulting string is machine-oriented
- Typically (not all) repr () results are: obj = = eval (repr (obj))
Formatting operator Helper
String formatting is possible with "%", but "%" is often used in conjunction with the following helper characters.
|
Auxiliary symbols |
Description |
|
* |
Define width or decimal precision |
|
- |
Used for left justification |
|
+ |
Show plus sign (+) in front of positive number |
|
# |
Display 0 (0) in front of octal numbers, "0x" or "0X" before hexadecimal (depending on "x" or "X") |
|
0 |
The displayed number is preceded by "0" instead of the default space |
|
(VAR) |
Mapping variables (typically used to process parameters of a field type) |
|
M.n |
M is the minimum total width displayed, and n is the number of digits after the decimal point (if available) |
Take a look at some simple examples:
num = 100Print "%d to hex are%x"%(num, num)Print "%d to hex are%x"%(num, num)Print "%d to hex is% #x"%(num, num)Print "%d to hex is% #X"%(num, num)#floating point numberf = 3.1415926Print "value of f is:%.4f"%F#Specify width and alignmentStudents = [{"name":"Wilber"," Age": 27}, {"name":" would"," Age": 28}, {"name":"June"," Age": 27}]Print "Name:%10s, Age:%10d"% (students[0]["name"], students[0][" Age"])Print "Name:%-10s, Age:%-10d"% (students[1]["name"], students[1][" Age"])Print "Name:%*s, Age:%0*d"% (students[2]["name"], ten, students[2][" Age"])#dict Parameters forStudentinchStudents:Print "% (name) s is% (age) d years old"%student
The code output is:
For Python's format operators, you can not only accept the parameters of the tuple type, but also support dict, like the last part of the code above, then you can use "% (key) S" directly in the format string. (s) represents the corresponding value in Dict, depending on the type of change.
String templates
In fact, formatting strings in Python, in addition to formatting operators, can also use String template objects in the string module. Here's a look at the substitute () method of the Template object:
fromStringImportTemplates= Template ("Hi, $name! $name is learning $language")PrintS.substitute (name="Wilber", language="Python") d= {"name":" would","language":"C #"}PrintS.substitute (d)#use $$ to represent the $ symbols = Template ("This book ($bname) is 17$$")PrintS.substitute (bname="TCP/IP")
The code results are:
Summarize
This article has compiled some formatting characters, as well as some auxiliary instructions, in combination with the format operator (%), you can generate a string in a specific format. You can also use string templates for string formatting.
Python formatted string