Python quick tutorial (Supplement 05): String formatting (% operator), python Operator

Source: Internet
Author: User

Python quick tutorial (Supplement 05): String formatting (% operator), python Operator

Many programming languages contain the function of formatting strings, such as formatting Input and Output in C and Fortran. Python has the built-in formatting operations on strings %.

 

Template

When formatting a string, Python uses a string as the template. There are format characters in the template, which are reserved locations for true real values and indicate the formats in which real values should be presented. Python uses a tuple to pass multiple values to the template. Each value corresponds to a format character.

For example:

Python
1 Print ("I'm % s. I'm % d year old" % ('vamei', 99 ))

In the above example,

"I'm % s. I'm % d year old" is our template. % S is the first formatted character, indicating a string. % D is the second formatted character, indicating an integer. The two elements 'vamei' and 99 of ('vamei', 99) are the actual values of % s and % d.
There is a % number between the template and tuple, which indicates the formatting 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 like a normal string. For example:

Python
12 A = "I'm % s. I'm % d year old" % ('vamei', 99) print ()

 

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': 99 })

We can see that two format characters are named. The name is enclosed. Each name corresponds to a dictionary key.

 

Format character

The format character is reserved for the real value, and the display format is controlled. A format character can contain a type code to control the display type, as shown below:

% S string (displayed using str)

% R string (display with repr)

% C single character

% B binary integer

% D decimal integer

% I decimal integer

% O octal integer

% X hexadecimal integer

% E index (base write as e)

% E index (base write as E)

% F floating point number

% F floating point number, same as above

% G index (e) or floating point number (based on the display length)

% G index (E) or floating point number (based on the display length)

 

% Character "%"

 

You can use the following method to further control the format:

% [(Name)] [flags] [width]. [precision] typecode

(Name) is named

Flags can include +,-, '', or 0. + Indicates the right alignment. -Indicates left alignment. ''Is a space, indicating that a space is filled on the left of a positive number to align with a negative number. 0 indicates filling with 0.

Width indicates the display width.

Precision indicates the decimal point precision.

 

For example:

Python
123 Print ("% + 10x" % 10) print ("% 04d" % 5) print ("% 6.3f" % 2.3)

 

The width and precision values above are two integers. We can use * to dynamically substitute these two quantities. For example:

Python
1 Print ("%. * f" % (4, 1.2 ))

Python actually uses 4 to replace *. Therefore, the actual template is "%. 4f ".

 

Summary

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 % operator is the most convenient to use.

All-round programmers exchange QQ Group 290551701 and gather many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.