Python string formatting

Source: Internet
Author: User
In simple terms, the Python string formatting operator (%) is very similar to the string formatting of the printf () function in C language, and even the symbols used are the same. the percent sign (%) is used ), all printf () formatting operations are supported. Syntax:

Format_string % string_to_convert

Format_string is a string in the format of "% cdoe"; string_to_convert is the string to be formatted. if there are more than two strings, they must be enclosed in parentheses.

Example of string formatting output:

CharA = 65 charB = 66 print ("ASCII code 65 stands for: % c" % charA) print ("ASCII code 66 stands for: % c" % charB) num1 = 0xEF3Num2 = 0xAB03print ('convert to decimal: % u and % u' % (Num1, Num2) Num3 = 1500000 print ('convert to scientific notation: % E' % Num3)

Output:

ASCII code 65:

ASCII code 66: B

Convert 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 convenient.

Auxiliary symbols, as shown in the following table:

Auxiliary symbol example:

Num1 = 108 print ("% # X" % Num1) Num2 = 234.567890 print ("%. 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.