05_ string Formatting

Source: Internet
Author: User

1 How to format strings
    • Percent Semicolon mode

    • Format test
200 semicolon format:%[(name)][flags][width]. [Precision]typecode2.1 Option Parameters
    • (name) "Optional" to select the specified key
    • Flags "Optional", the values to choose from are:
+ Right-aligned, positive plus, negative before plus minus; -left-justified; no sign before positive number, plus minus sign before negative number; spaces are right-justified, plus a positive number before a negative number plus a minus sign; 0 Right alignment, no sign before positive number, minus sign before negative, and 0 padding in blank
    • Width "optional", occupied widths
    • . Precision "Optional", number of digits retained after the decimal point
    • TypeCode "must choose"
S, gets the return value of the __str__ method of the passed-in object and formats it to the specified locationR, gets the return value of the __repr__ method of the passed-in object and formats it to the specified locationC, Integer: Converts a number to its Unicode corresponding value, 10 binary range is 0 <= i <= 1114111 (py27 only supports 0-255); character: add character to specified positionO, converts an integer to an octal representation and formats it to a specified locationX, converts an integer to a hexadecimal representation and formats it to a specified locationD, converts an integer, floating-point number to a decimal representation, and formats it to a specified positionE, converting integers, floating-point numbers to scientific notation, and formatting them to a specified location (lowercase e)E, converting integers, floating-point numbers into scientific notation, and formatting them to a specified position (uppercase 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 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;)%, when there is a format flag in the string, a percent sign is required

Note: There is no way to automatically convert an integer to a binary representation in Python with a percent-semicolon format

2.2 Practice

1) Common string formatting exercises

#%s Formatting the string to the specified position TP = "I am%s"% "Alex" Print (TP) # Output I am alex#%d converts an integer, floating-point number to a decimal representation, and formats to a specified position TP = "I am%s age%d"% ("ale X ", +) print (TP) # Output I am Alex age 18#% (name) formats the value of the specified key in the dictionary to the specified position TP =" I am% (name) s age% (age) d "% {" name ":" Alex "," AG E ": 18}print (TP) # Output I am Alex age 18#%f converts an integer, floating-point number to a floating-point number, retains the decimal point two bits, and formats it to the specified location (the default is save 6 digits after the decimal point) TP =" percent%.2f "% 99.97623pri  NT (TP) # Output percent 99.98#% () +%f Specifies the value of the dictionary key, the result retains two decimal places (rounded), formatted to the specified position TP = "I am% (PP)." 2f "% {" pp ": 123.425556,}print (TP) # Output I am 123.43# percent when there is a format flag% in the string, it is necessary to represent a%tp = "I am%.2f percent"% 123.425556print (TP) # output result I am 123.43%  # output A%, Equivalent to escaping

2) Other string formatting exercises

#%c Converts a number to a Unicode corresponding value #%o Converts an integer to octal notation #%x converts an integer to a hexadecimal representation TP = "Test%c---  %o---%x"% (65,8,15) print (TP) # output # test A---  ---F #%e%e Science Count TP = "Test%e---  %e"% (100000000000000000,100000000000000000) print (TP) # test 1.000000e+17---  1.000000E+17 #%g Automatic adjustment converts integers, floating-point to floating-point or science, more than 6-bit conversion scientific notation, e#%g automatic adjustment converts integers, floats to floating-point open or scientific, more than 6-bit conversion scientific notation, ETP = "Test%g---  %g"% ( 9999.999,9999999999.9999999) Print (TP) # TEST 10000---  1E+10TP = "Test%G---  %G"% (9999.999,9999999999.9999999 ) print (TP) # TEST 10000---  1E+10

  

3 format

Format: [[Fill]align][sign][#][0][width][,][.precision][type]

3.1 Option parameters
    • Fill "optional" white space filled with characters
    • Align "optional" alignment (required with width)
< content left Justified> Right align Content (default)= content is right-aligned, placing the symbol to the left of the fill character, and only valid for numeric types. Even: symbol + filler + number^ Content Centered
    • Sign "optional" with unsigned numbers
+ Positive home Plus, minus plus negative;-The plus sign is constant, minus is negative;Empty line space, minus sign plus negative;
    • # "optional" for binary, octal, hex, if added #, 0b/0o/0x is displayed, otherwise not displayed
    • , "optional" adds a delimiter to the number, such as: 1,000,000
    • width "Optional" format the size of the placeholder
    • . Precision "optional" decimal digits reserved Precision
    • Type "optional" formatting types
Parameters passed into the string type:s, format string type dataBlank, no type specified, default is None, same as SParameters passed into the integer type:B, automatic conversion of 10 binary integers to 2 binary representation and then formattingC, automatic conversion of 10 binary integers to their corresponding Unicode charactersd, decimal integerO, the 10 binary integers are automatically converted to 8 binary representation and then formatted;X, automatically converts 10 binary integers to 16 binary representation and then formats (lowercase x)X, automatically converts 10 binary integers to 16 binary representation and then formats (uppercase X)Parameters passed in floating-point or decimal type:E, converted to scientific notation (lowercase e), and then formatted;E, converted to scientific notation (capital E), and then formatted;F, converted to floating-point type (6 digits after the default decimal point), and then formatted;F, converted to floating-point type (6 digits after the default decimal point), and then formatted;G, automatically switch between E and FG, automatically switch between E and F%, display percent (default 6 digits after decimal)
3.2 Practice

1) Common formatting

# to fill blanks with characters, specify string width (20), center (^) display, decimal integer, 16 binary integer s2 = "---{:* ^20s}==={:+d}==={:x}". Format (' Alex ', 123, page) print (s2) #---  alex********===+123===f# the corresponding formatted string in the left-to-right order TP1 = "I am {}, age {}, {}". Format ("Seven", +, ' Alex ') print (TP1) # I am  Seven, age, alex# Pass in the corresponding formatted list in order from left to right, make sure to add *TP2 before the list = "I am {}, age {}, {}". Format (*["Seven", "Alex") print (TP2) # I am  Seven, age, alex# Pass in the corresponding formatted string in ordinal order TP3 = "I am {0}, age {1}, really {0}". Format ("seven", +) print (TP3) # I am seven, age Really seven# to the corresponding formatted list by index, must be added *TP4 = "I am {0}, age {1}, really {0}". Format (*["seven", +]) print (TP4) # I am Seve  N, age, really seven# format content by variable name TP5 = "I am {name}, age {age}, really {name}". Format (name= "Seven", age=18) print (TP5) # I Am Seven, age, really seven# format content by dictionary key value TP6 = "I am {name}, age {age}, really {name}". Format (**{"name": "Seven", "age"  :) print (TP6) # I am seven, age, really seven# formatted string by index index in TP7 = "I am {0[0]}, age {0[1]}, really {0[2]}". Format ([1, 2, 3],[11, +]) print (TP7)# I am 1, age 2, really 3#tp8 = "I am {: s}, age {:d}, Menoy {: F}". Format ("Seven", +, 8888.1) print (TP8) # I am seven, age Menoy 8888.100000#TP9 = "I am {: s}, age {:d}". Format (*["seven", +]) print (TP9) # I am seven, age 18#tp10 = "I am {name: s}, age {age:d} '. Format (name= "Seven", age=18) print (TP10) # I am seven, age 18#tp11 = "I am {name:s}, age {age:d}". Format (* * {"Name": "Seven", "Age": 18}) Print (TP11) # I am seven, age 18# b binary, o octal, D decimal, x 16 binary, x hexadecimal (result uppercase),% display percent TP12 = "Numbers: {: b},{:o},{:d},{:x},{:x}, {:%}" . Format (15.87623, 2) print (TP12) # numbers:1111,17,15,f,f, 1587.623000%# by index format tp13 = "Numbers: {0:b},{ 0:o},{0:d},{0:x},{0:x}, {0:%} ". Format (page) print (TP13) # numbers:1111,17,15,f,f, 1500.000000%# tp14 =" numbers: {num:b}, {num:o},{num:d},{num:x},{num:x}, {num:%} '. Format (num=15) print (TP14) # numbers:1111,17,15,f,f, 1500%

  

05_ string Formatting

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.