Detailed description of Python string formatting

Source: Internet
Author: User
The string formatting operator (%) is very similar to the string formatting of the printf () function in C, and even the symbols used are the same, all with percent sign (%), and all printf () format operations are supported. The syntax is as follows:

Format_String% String_to_convert

Format_String is a format-tagged string, in the form "%cdoe"; String_to_convert is a string to be formatted, and if it is more than two, it needs to be enclosed in parentheses.


String formatting symbols

Formatting symbols Description
%c Convert to character (ASCII value, or string of one length)
%s Convert to string, precedence string conversion with STR () function
%d Turn into a signed decimal number
%u Turn into unsigned decimal numbers
%o Turn unsigned octal number
%x (Unsigned) turns into unsigned hexadecimal numbers
%e Turn into scientific counting method
%% Output
%x (Unsigned) to unsigned hexadecimal number, converted to 16 characters in uppercase, similar to%e (lowercase after conversion)

Example of string format output:

CharA = 65charB = 66print ("ASCII code 65 for:%c"% CharA) print ("ASCII code 66 stands for:%c"% charb) Num1 = 0xef3num2 = 0xab03print (' converted to decimal: %u and%u '% (NUM1, Num2)) Num3 = 1500000print (' Convert to scientific notation:%e '% Num3)

Output:

ASCII code 65 means: A

ASCII code 66 means: B

Converted to decimal: 3827 and 43779, respectively

Convert to scientific notation: 1.200000e+06


Formatting characters can also be used with auxiliary symbols, which is very handy.

Auxiliary symbols, as shown in the following table:

Auxiliary symbols Description
* Define width or decimal precision
- Used for left justification
+ Show plus sign (+) in front of positive number
<sp> Show spaces in front of positive numbers
# Display 0 (0) in front of octal numbers, "0x" or "0X" before hexadecimal (depending on "x" or "X")
0 The displayed number is preceded by "0" instead of the default space
M.n is the minimum total width displayed, and n is the number of digits after the decimal point

Note: The secondary symbol is to be between the percent sign (%) and the formatting symbol.


Examples of auxiliary symbols:

NUM1 = 108print ("% #X"% Num1) Num2 = 234.567890print ("%.2f"% Num2)

Output:

0x6c

234.57

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.