Python string formatting

Source: Internet
Author: User
Tags printf python string format string format in python

The Python string format operator (%) applies only to string types, very similar to the string formatting of the printf () function in the C language, and even the symbols used are the same as the percent sign (%), and all of the printf ()-formatted operations are supported.

Features formatted strings are included in many programming languages, such as formatted input and output in C and Fortran languages. In Python, there is a built-in operation to format strings.

Template

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

For example, the following examples:

The code is as follows:

Print (' I ' m%s. I ' m%d year old '% (' Vamei ', 99))

In the example above,

"I ' m%s. I ' m%d year old ' for our template. %s is the first format character, representing a string. %d is the second format character, representing an integer. The two elements ' Vamei ' and 99 (' Vamei ', 99) are the true values for the replacement of%s and%d.

Between the template and the tuple, there is a% number separator, which represents the formatting operation.

Entire ' I ' m%s. I ' m%d year old '% (' Vamei ', 99) actually constitutes a string expression. We can assign it to a variable like a normal string. Like what:

The code is as follows:

A = "I ' m%s." I ' m%d year old '% (' Vamei ', 99)

Print (a)

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

The code is as follows:

Print ("I ' m% (name) s." I ' m% (age) D-old '% {' name ': ' Vamei ', ' Age ': 99})

As you can see, we've named two format characters. Name is enclosed with (). A key for each named corresponding dictionary.

Format character

The format character reserves the position for the true 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 (with str () display)

%r string (shown with repr ())

%c single character

%b binary integer

%d decimal integer

%i Decimal Integer

%o octal integers

%x Hex Integer

%e index (base written as E)

%E index (base written as E)

%f floating-point numbers

%F floating-point number, same as on

%g index (e) or floating-point number (depending on the display length)

%G Index (E) or floating-point number (depending on the display length)

Percent% character "%"

You can further control the format in the following ways:

%[(name)][flags][width]. [Precision]typecode

(name) is named

Flags can have +,-, ' or 0. + Represents the right alignment. -Represents the left alignment. "is a space that indicates that a space is padded to the left of a positive number to align with a negative number. 0 indicates the use of 0 padding.

Width represents display widths

Precision indicates precision after a decimal point

Like what:

The code is as follows:

Print ("%+10x"% 10)

Print ("%04d"% 5)

Print ("%6.3f"% 2.3)

The width above, precision to two integers. We can use the * to dynamically substitute these two quantities. Like what:

The code is as follows:

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

Python actually replaces * with 4来. So the actual template is "%.4f".

Example:

The code is as follows:

In [1]: '% (name) s is% (age) s '% {' name ': ' Bob ', ' Age ': 12}

OUT[1]: ' Bob is 12 '

In [2]: '%s is%s '% ("Bob", "13")

OUT[2]: ' Bob is 13 '

In [6]: ' {0} is {1} '. Format ("Bob", "14")

OUT[6]: ' Bob is 14 '

In [7]: ' {name} ' is ' {age} '. Format (name= "Bob", age= "15")

OUT[7]: ' Bob is 15 '

Summarize

The% operator built into Python can be used to format string manipulation and control the rendering format of strings. There are other ways to format strings in Python, but the% operator is the easiest to use.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.