In many programming languages, there is the ability to format strings, such as formatted input and output in C and Fortran languages. Python has a built-in operation to format the string.
Template
When formatting a string, Python uses a string as a template. There are formatting characters in the template that reserve locations for real values and describe the format in which real values should be rendered. Python uses a tuple to pass multiple values to the template, each of which corresponds to a format character.
For example, the following:
Python
1 |
Print("I ' m%s.") I ' m%d year old " % (' Vamei '," ) ") |
In the example above,
"I ' m%s. I ' m%d year old "for our template. %s is the first format character, which represents a string. %d is the second format character, which represents an integer. (' Vamei ', 99) Two elements ' Vamei ' and 99 are substituted for the true values of%s and%d.
Between the template and the tuple, there is a% number separated, which represents the format operation.
The entire "I ' m%s. I ' m%d year old "% (' Vamei ', 99) actually forms a string expression. We can assign a value to a variable as if it were a normal string. Like what:
Python
12 |
a = "I ' m%s." I ' m%d year old " % (' Vamei ' ) print(a) |
We can also use dictionaries to pass real values. As follows:
Python
1 |
Print("I ' m% (name) s. I ' m% (age) D-year-old " % {' name ':' Vamei ', ' age ': ()}) |
As you can see, we have named two of the format characters. The name is used () enclosed. A key for each named corresponding dictionary.
Format character
The format character reserves the location for the real value and controls the format of the display. A format character can contain a type code that controls the type of display, as follows:
%s string (shown with STR ())
%r string (shown with repr ())
%c single character
%b binary integers
%d decimal integers
%i Decimal Integer
%o Eight-binary integers
%x hexadecimal integer
%e index (base written as E)
%E index (base written as E)
%f floating Point
%F floating-point number, same as above
%g index (e) or floating point number (depending on display length)
%G Index (E) or floating point number (depending on display length)
Percent% character "%"
The format can be further controlled in the following ways:
%[(name)][flags][width]. [Precision]typecode
(name) is named
Flags can have +,-, ' or 0. + Indicates right alignment. -Indicates left alignment. ' is a space that fills a space on the left side of a positive number to align with a negative number. 0 means using 0 padding.
Width indicates display widths
Precision indicates the precision after the decimal point
Like what:
Python
123 |
Print("%+10x" % ) Print("%04d" % 5) Print("%6.3f" % 2.3) |
The width above, precision is two integers. We can use * to dynamically substitute these two quantities. Like what:
Python
1 |
Print("%.*f" % (4, 1.2)) |
Python actually replaces * with 4来. So the actual template is "%.4f".
Summarize
The built-in% operator in Python can be used to format string operations and control the rendering format of strings. There are other ways to format strings in Python, but the use of the% operator is most convenient.
All-in-one programmer Exchange QQ Group 290551701, gather a lot of Internet elite, technical director, architect, Project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!
Python Quick Tutorials (Supplemental 05): String formatting (% operator)