String formatting (% operator)

Source: Internet
Author: User
Many programming languages contain the function of formatting strings, such as formatting input and output in C and Fortran. The built-in operators for formatting strings in Python are & amp; quot; % & amp; amp; quot ;. Many programming languages contain the function of formatting strings, such as formatting input and output in C and Fortran. The built-in operator for formatting strings in Python is "% ".

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:

Print ("I'm % s. I'm % d" % ('pythontab', 1 ))

In the above example,

"I'm % s. I'm % d" is our template. % S is the first formatted character, indicating a string. % D is the second formatted character, indicating an integer. ('Pythontab', 99) the two elements 'pythontab' and 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" % ('pythontab', 1) actually forms a string expression. We can assign a value to a variable like a normal string. For example:

A = "I'm % s. I'm % d" % ('pythontab', 1)

Print ()

The result is: I'm Pythontab. I'm 1.

We can also use dictionaries to pass real values. As follows:

Print ("I'm % (name) s. I'm % (age) d" % {'name': 'pythontab', '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:

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:

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.

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.