The path to Python "third":P the Ython base of the grocery store

Source: Internet
Author: User

string Formatting

There are two ways to format a Python string: a percent-semicolon, format-mode

The percent of the semicolon is relatively old, and format is a more advanced way to replace the old way, the two coexist.

1, percent of the way
%[(name)][flags][width]. [Precision]typecode
(name) optional to select the specified key flags to choose from: + Right-aligned, positive plus, negative before plus minus;-left justified; positive front unsigned , negative number before plus minus, white space right alignment, positive before plus space, negative number before plus minus; 0 right alignment, positive sign without sign, negative before plus minus; 0 padding width .precisio         n Optional, the number of digits retained after the decimal point TypeCode required S, gets the return value of the __str__ method of the incoming object, formats it to the specified position R, gets the return value of the __repr__ method of the passed object, and formats it to the specified location C, Integer: Converts a number to its Unicode corresponding value, 10 binary range is 0 <= i <= 1114111 (py27 only supports 0-255); character: Adds a character to the specified position O, converts an integer to an octal representation, and Formats it to the specified position x, converts the integer to a hexadecimal representation, and formats it to the specified position d, converts the integer, floating-point number to a decimal representation, and formats it to the specified position E, converts the integer, floating-point number to scientific notation, and formats it to        Specifies the position (lowercase e) E, converts an integer, floating-point number to scientific notation, and formats it to the specified position (capital E) F, converts an integer, floating-point number to a floating-point number representation, and formats it to a specified position (the default is 6 digits after the decimal point) F Ibid.        G, auto-adjust convert integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and format them to a specified location (if scientific counts are e;)        G, auto-adjust to integers, floating-point numbers converted to floating-point or scientific notation (more than 6 digits in scientific notation), and formatted to the specified location (if the scientific count is E;) %, when there is a format flag in the string, a percent sign is requiredNote: In Python, the format of the percent semicolon does not exist in the way that an integer is converted to a binary representation    

Common formatting:

msg ="I am%s My hobbby is%s"%('William','Basketball')Print(msg) Output: I am william my hobbby isbasketballmsg='I am%s, age%s'%('William', 18)Print(msg) Output: I am William, age18msg='I am% (name) s age percent (age) D'% {'name':'William',' Age': 19}Print(msg) Output: I am William age19#Print floating-point numbersTPL ="percent%.2f"% 99.97623445463344Print(TPL) output result: Percent99.98#Print PercentageTPL ="percent%.2f"% 99.97623445463344Print(TPL) output result: Percent99.98%msg='I am% (name) +60s my hobby is basketball.'% {'name':'William'}Print(msg) Output: I am william my hobby isbasketball.msg='I am \033[43;1m% (name) +60s\033[0m my hobby is basketball.'% {'name':'William'}Print(msg) Output: I am william my hobby isbasketball.Print('Root','x','0','0', sep=':') Output Result: root:x:0:0

2, format mode expression:
[fill]align][sign][#][0][width][,][.precision][type]
Common formatting:

msg ='I am {},age {}, {}'. Format ('Zhurui', 24,'William')Print(msg) Output: I am zhurui,age24, Williammsg='I am {},age {}, {}'. Format (*['Zhurui', 24,'William'])Print(msg) Output: I am zhurui,age24, Williammsg='I am {0}, age {1},really {0}'. Format ('Zhurui', 24)Print(msg) Output: I am Zhurui, age24, really zhuruimsg='I am {0}, age {1},really {0}'. Format (*['Zhurui', 24])Print(msg) Output: I am Zhurui, age24, really zhuruimsg='I am {name}, age {age},really {name}'. Format (name='William', age=24)Print(msg) Output: I am William, age24, really williammsg='I am {name}, age {age},really {name}'. Format (**{'name':'William',' Age': 24})#format if you use a dictionary, the front plus * *Print(msg) Output: I am William, age24, really williammsg='i am{0[0]}, age {0[1]}, really {0[2]}'. format ([1, 2, 3], [11, 22, 33])Print(msg) Output: I am1, age2, really 3msg='I am {: s}, age {:d}, Money {: F}'. Format ('William', 24,89.3432)Print(msg) Output: I am William, age89.343200, Moneymsg='I am {: s},age {:d}'. Format (*['William', 24])Print(msg) Output: I am william,age24msg='I am {name:s},age {age:d}'. Format (**{'name':'William',' Age': 24})Print(msg) Output: I am william,age24msg="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2)Print(msg) Output: Numbers:1111,17,15,f,f, 1587.623%msg="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) Output Result: Numbers:1111,17,15,f,f, 1587.623%msg="numbers: {0:b},{0:o},{0:d},{0:x},{0:x}, {0:%}". Format (15)Print(msg) Output: Numbers:1111,17,15,f,f, 1500%msg="numbers: {num:b},{num:o},{num:d},{num:x},{num:x}, {num:%}". Format (num=15)Print(msg) Output: Numbers:1111,17,15,f,f, 1500%

The path to Python "third":P the Ython base of the grocery store

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.