Preface
Python has added a new string formatting method to version 2.6: str.format() . Its basic syntax is to replace the previous% with {} and:.
Placeholder syntax when formatting:
Replacement_field:: = "{" [Field_name] ["!" conversion] [":" Format_spec] "}"
Map rule
By location
str.format() You can accept an unlimited parameter, and the position can be out of order:
>>> ' {0} {1} '. Format ("Hello", "world") ' Hello World ' >>> ' {} {} '. Format ("Hello", "world") ' Hello World ' >>> "{1} {0} {1}". Format ("Hello", "World") ' World Hello World '
by keyword parameter
Parameter names are required in a string when using key parameters:
>>> "I am {name}, age was {age}". Format (name= "Huoty", age=18) ' I am huoty, age is ' >>> user = {"Name": " Huoty "," Age ": 18}>>>" I am {name}, age was {age} ". Format (**user) ' I am huoty, age is 18 '
Through object properties
str.format() User properties can be read directly:
>>> class User (object): ... def __init__ (self, Name, age): ... Self.name = Name ... Self.age = Age ... ... def __str__ (self): ... Return "{Self.name} ({self.age})". Format (self=self) ... ... def __repr__ (self): ... Return self.__str__ () ... ...>>> user = User ("Huoty") >>> Userhuoty (+) >>> "I am {user.name}, age is {user.age}". Form at (user=user) ' I am Huoty, age is 18 '
by subscript
The element can be accessed by subscript within a string that needs to be formatted:
>>> names, ages = ["Huoty", "Esenich", "Anan"], [+], 8]>>> "I am {0[0]}, age is {1[2]}". Format (names , ages) ' I am Huoty, age is 8 ' >>> users = {"Names": ["Huoty", "Esenich", "Anan"], "ages": [+], 8]}>>> "I am {names[0]}, age is {Ages[0]}". Format (**users)
Specify conversions
You can specify the conversion type of the string:
Conversion:: = "R" | "S" | A
where "!r" corresponds to Repr (); "!s" corresponds to STR (); "!a" corresponds to ASCII (). Example:
>>> "repr () shows quotes: {!r}; STR () doesn ' t: {!s} '. Format (' test1 ', ' test2 ') "repr () shows quotes: ' Test1 '; STR () doesn ' t:test2 '
Format qualifier
Fill and align
Padding is used in conjunction with alignment. ^, <, > is centered, left-aligned, right-aligned, followed by the width of the character, after the number of characters, can only be one character, not specified by default is filled with spaces.
>>> "{: >8}". Format ("181716") ' 181716 ' >>> ' {: 0>8} '. Format ("181716") ' 00181716 ' >>> ' {:->8} ". Format (" 181716 ") '--181716 ' >>> ' {:-<8} '. Format (" 181716 ") ' 181716--' >>> ' {:-^8} '. Format ("181716") ' -181716-' >>> ' {:-<25}> '. Format ("Here") ' Here--------------------> '
Floating point Precision
The floating-point type is represented by F and can be preceded by a precision control:
>>> "[{:. 2f}]". Format (321.33345) ' [321.33] ' >>> "[{:. 1f}]". Format (321.33345) ' [321.3] ' >> > "[{:. 4f}]". Format (321.33345) ' [321.3335] ' >>> [{:. 4f}] ". Format (321) ' [321.0000] '
You can also specify a symbol for a floating-point number, + to display a + before a positive number, a negative number before it is displayed-; (a space) to indicate a space before a positive number and a-;-with nothing ({: F}) before a negative number:
>>> ' {: +f}; {: +f} '. Format (3.141592657,-3.141592657) ' +3.141593; -3.141593 ' >>> ' {: F}; {: F} '. Format (3.141592657,-3.141592657) ' 3.141593; -3.141593 ' >>> ' {: F}; {: F} '. Format (3.141592657,-3.141592657) ' 3.141593; -3.141593 ' >>> ' {:-F}; {:-F} '. Format (3.141592657,-3.141592657) ' 3.141593; -3.141593 ' >>> ' {: +.4f}; {: +.4f} '. Format (3.141592657,-3.141592657) ' +3.1416; -3.1416 '
Specify the binary
>>> "int: {0:D}; Hex: {0:x}; Oct: {0:o}; Bin: {0:b} ". Format (+) ' int:18; Hex:12; oct:22; bin:10010 ' >>> ' int: {0:D}; Hex: {0: #x}; Oct: {0: #o}; Bin: {0: #b} ". Format (+) ' int:18; hex:0x12; Oct:0o22; bin:0b10010 '
Thousands separator
You can use "," as the Thousand separator:
>>> ' {:,} '. Format (1234567890) ' 1,234,567,890 '
Percent display
>>> "Progress: {:. 2%}". Format (19.88/22) ' progress:90.36% '
In fact, format also supports more type symbols:
Type:: = "B" | "C" | "D" | "E" | "E" | "F" | "F" | "G" | "G" | "N" | "O" | "S" | "X" | "X" | "%"
Other Tips
Placeholder Nesting
Placeholder nesting is useful at some point:
>>> ' {0:{fill}{align}16} '. Format ("Hello", fill= ' * ', align= ' ^ ') ' *****hello****** ' >>>>>> For NUM in range (5,12): ... For base in "Dxob": ... Print ("{0:{width}{base}}". Format (num, base=base, width=5), end= ") ... Print () ... ... 5 5 5 101 6 6 6 The 7 7 7 111 8 8, 9 9, 1001 A, 1010, B 13 1011
Use as a function
Instead of specifying a format parameter, you can call it in the wrong place as a function:
>>> email_f = "Your Email address was {email}" .format>>> print (Email_f (email= "suodhuoty@gmail.com")) Your Email address was sudohuoty@gmail.com
Escape curly Braces
You can escape curly braces when you need to use curly braces in a string:
>>> "The {} set is often represented as {{0}}". Format ("Empty") ' The empty set is often represented as {0} '