python--the Format function with a string

Source: Internet
Author: User
Tags string format

Before you say the Format function, talk about formatting a string in Python, usually by using a% (percent semicolon) to format the string in a way that is similar to the format string in the C language

1. Format the string by using the% (mode:% string format Controller) to operate in Ipython

Common string format controls are:

%c character and its ASCII code

%s string

%d signed integer (decimal)

%u unsigned integer (decimal)

%o unsigned integer (octal)

%x unsigned integer (hex)

%x unsigned integer (16 in uppercase characters)

%e floating-point number (scientific counting method)

%e floating-point number (scientific notation, substituting E for e)

%f floating-point number (with decimal point symbol)

%g floating-point numbers (using%e or%f depending on the size of the value)

%g floating-point numbers (similar to%g)

%p pointer (memory address with hexadecimal print value)

%n the number of stored output characters into the next variable in the argument list

2.% can also format strings by naming arguments, which is somewhat similar to the format you'll be talking about in the following way

3. From the beginning of python2.6 Python supports the use of the Str.format () function to format strings, using the following example to understand the Format function

3.1 Pass Position parameter

Here you need to leave a little bit is the second, the position parameter all blank, when all is empty, he automatically matches in order

The Format function of a string can accept an unlimited number of arguments (this argument can be either a single string or a sequence), the position can be out of order, may not be used or repeated (e.g. the third above), However, it is not possible to be empty in 2.6, but support for 2.7 and above, including 3.x, can be null (for example, the second in the example above).

3.2 Through keyword parameters

3.3 Through Map list

3.4 By Mapping Dict

4. Through the format qualifier, the basic syntax is {} with: number

4.1 Padding and alignment:

The ^,>,< is centered, right-aligned, left-aligned, followed by the width below: The filled character followed by the number, can only be one character, and if not specified, the default padding is a space look example:

{: >8} represents a right-aligned eight-bit, but there is no padding character specified behind the: number, so by default, padding is done by using a space, which is compared to the following example stroke

These are some common uses of the format function





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.