python string formatting
Python string formatting in two ways:% formatter mode, format mode
% Formatter
%[(name)][flags][width].[precision]typecode
- (Name) Alternatively, for selecting a specified key
- flags optional, alternative values are:
- + Right aligned; added just before the positive, negative numbers preceded by a minus sign;
- - Left; unsigned positive front, a minus sign before the negative;
- Right-aligned spaces; spaces, a minus sign before the negative before positive number;
- Align Right 0; unsigned before positive and negative numbers preceded by a minus sign; the space filled with 0
- width optional, possession of width
- .precision optional decimal places reserved
- typecode Required
- s, __str__ method of obtaining the return value of the incoming object and is formatted to the specified position
- r, __repr__ method of obtaining the return value of the incoming object and is formatted to the specified position
- C, integers: a number into its corresponding unicode values, decimal range 0 <= i <= 1114111 (py27 only supports 0-255); character: adding a character to the specified position
- O, the octal notation is converted into an integer, and formats it into the designated location
- x, an integer converted into hexadecimal notation, and formats it into the designated location
- d, convert integer, floating point decimal representation and formats it to a designated position
- E, integer, floating-point numbers into scientific notation, and formats it into the designated location (lowercase e)
- E, converting the integer, floating-point number in scientific notation, and formats it to the specified position (uppercase E)
- F, converting integers, floating point numbers to floating point representation and formats it to the specified position (after the default 6 decimal places)
- F, ibid.
- g, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
- G, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
- %, When the flag is present formatted string, a percent sign indicates required by %%
Note: Python the percent format is automatically converted into a binary representation of an integer as not exist
(Name) Alternatively, for selecting a specified key
a = "%(name)s-----%(age)d "%{'name':'xx','age':20} print(a)
Results of the:
xx-----20
flags optional, alternative values are:
- + Right aligned; added just before the positive, negative numbers preceded by a minus sign;
- - Left; unsigned positive front, a minus sign before the negative;
- Right-aligned spaces; spaces, a minus sign before the negative before positive number;
- Align Right 0; unsigned before positive and negative numbers preceded by a minus sign; the space filled with 0
width optional, possession of width
account name 10, +, right justified, age accounted for 10, -, Left
b = "%(name)+10s————————%(age)-10d————————"%{'name':'xx','age':20} print(b)
Results of the:
xx————————20 ————————
Space, right-aligned
0, the space is filled with 0
c = "------%(year) d******%(age)010d "%{'year':2016,'age':-20} print(c)
Results of the:
------ 2016******-000000020
.precision optional decimal places reserved
Only two decimal places
d = '--------%(p).2f'%{'p':1.23456} d1 = '--------%(p)f'%{'p':1.23456} print(d) print(d1)
Results of the:
--------1.23 --------1.234560
typecode Required
- C, integers: a number into its corresponding unicode values, decimal range 0 <= i <= 1114111 (py27 only supports 0-255); character: adding a character to the specified position
- O, the octal notation is converted into an integer, and formats it into the designated location
- x, an integer converted into hexadecimal notation, and formats it into the designated location
e = 'c *** ***%%% *** the x'% (65,15,15) print (e)
Results of the:
***A***17***f
- E, integer, floating-point numbers into scientific notation, and formats it into the designated location (lowercase e)
- E, converting the integer, floating-point number in scientific notation, and formats it to the specified position (uppercase E)
f = '-----% (num) ------% of (whether) E'% { 'or' 1000000000} print(f)
Results of the:
-----1.000000e+09------1.000000E+09
- g, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
- G, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
g '-----% (num) g ------% (num1) G'% { 'whether', 1000000000, 'num1': 100} print(g)
Results of the:
-----1e+09------100
- %, When the format string flag is present, it is necessary %% represented by a percent sign (similar to turn back effect)
s = 'aaa %' print(s) s1 = 'aaa %s %%'%('bbb') print(s1)
Results of the:
aaa % aaa bbb%
format mode
Defined digital format to: Start Number '. I met ':' characters know you want to define a number of display formats. Defined as sequential format
[[fill]align][sign][#][0][width][,][.precision][type]
- [Optional] fill the space filled with character
- [optional] align alignment (in conjunction with the use of width)
- <, Left-aligned content
- > Contents in the right alignment (default)
- = Contents in the right alignment, the symbol on the left pad characters, and only valid digital type. Even: digital sign + filler +
- ^, Centered content
- sign [optional] whether the symbol numbers
- +, Positive plus plus plus minus minus sign;
- - a positive sign change, plus minus negative sign;
- Plus negative space, plus space, negative sign;
- # [Optional] For binary, octal, hexadecimal, if coupled with # displayed 0b / 0o / 0x, or do not show
- , [Optional] is added to a digital separators, such as: 1,000,000
- [optional] format bit width occupied by the width
- .precision [optional] decimal place precision reserved
- type [optional] format type
- Incoming "string type" parameter
- s, string type data format
- Blank, unspecified type, the default is None, with s
- Incoming "integer type" parameter
- b, will be automatically converted to decimal integer of 2 hexadecimal format then
- c, will automatically convert a decimal integer to its corresponding unicode character
- d, decimal integer
- o, it will be automatically converted to decimal integer octal and format;
- x, the decimal integer automatically converted into a hexadecimal format and (lower case x)
- X, will be automatically converted to decimal integer in hexadecimal format and then (upper case X)
- Passed "or decimal floating-point type" parameter
- E, is converted to scientific notation (lowercase e) shows, then format;
- E, is converted to scientific notation (uppercase E), and format;
- F, is converted to floating point (the default after the decimal point 6) shows, then format;
- F., Is converted to floating point (the default after the decimal point 6) shows, then format;
- g, e and f are automatically switched
- G, automatically switches E and F
- %, Shows the percentage (default display 6 decimal place)
- Incoming "string type" parameter
[Optional] fill the space filled with character
[optional] align alignment (in conjunction with the use of width)
- <, Left-aligned content
- > Contents in the right alignment (default)
- = Contents in the right alignment, the symbol on the left pad characters, and only valid digital type. Even: digital sign + filler +
- ^, Centered content
[optional] format bit width occupied by the width
s1 ='---{:*^20s}----'.format('welcome') print(s1) s2 ='---{:*>20s}----'.format('welcome') print(s2) s3 ='---{:*<20s}----'.format('welcome') print(s3)
Results of the:
---******welcome*******---- ---*************welcome---- ---welcome*************----
# [Optional] For binary, octal, hexadecimal, if coupled with # displayed 0b / 0o / 0x, or do not show
- b, will be automatically converted to decimal integer of 2 hexadecimal format then
- c, will automatically convert a decimal integer to its corresponding unicode character
- d, decimal integer
- o, it will be automatically converted to decimal integer octal and format;
- x, the decimal integer automatically converted into a hexadecimal format and (lower case x)
- X, will be automatically converted to decimal integer in hexadecimal format and then (upper case X)
Three methods of representation
a1 = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{:c}".format(15, 15, 15, 15, 15, 15.87623,65) a2 = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%},{1:c}".format(15,65) A3 = "numbers: {Surely, b}, {whether: o}, {Surely, d}, {whether x}, {Surely, 10}, {num%}, {c, c}". format ( Surely = 15, C = 65) print(a1) print(a2) print(a3)
Results of the:
numbers: 1111,17,15,f,F, 1587.623000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
, [Optional] is added to a digital separators, such as: 1,000,000
.precision [optional] decimal place precision reserved
n = '{---} ---- :, d. size (10000000) n1 = '--- {:. 2f} ----'. format (1.2345) print(n) print(n1)
Results of the:
---10,000,000---- ---1.23----
format commonly used format
tp1 = "i am {}, age {}, {}".format("seven", 18, 'alex') tp2 = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) tp3 = "i am {0}, age {1}, really {0}".format("seven", 18) tp4 = "i am {0}, age {1}, really {0}".format(*["seven", 18]) tp5 = "i am {name}, age {age}, really {name}".format(name="seven", age=18) tp6 = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) tp7 = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) tp8 = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) tp9 = "i am {:s}, age {:d}".format(*["seven", 18]) tp10 = "i am {name:s}, age {age:d}".format(name="seven", age=18) tp11 = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) print(tp1) print(tp2) print(tp3) print(tp4) print(tp5) print(tp6) print(tp7) print(tp8) print(tp9) print(tp10) print(tp11)
Results of the:
i am seven, age 18, alex i am seven, age 18, alex i am seven, age 18, really seven i am seven, age 18, really seven i am seven, age 18, really seven i am seven, age 18, really seven i am 1, age 2, really 3 i am seven, age 18, money 88888.100000 i am seven, age 18 i am seven, age 18 i am seven, age 18
Python string formatting in two ways:% formatter mode, format mode
% Formatter
%[(name)][flags][width].[precision]typecode
- (Name) Alternatively, for selecting a specified key
- flags optional, alternative values are:
- + Right aligned; added just before the positive, negative numbers preceded by a minus sign;
- - Left; unsigned positive front, a minus sign before the negative;
- Right-aligned spaces; spaces, a minus sign before the negative before positive number;
- Align Right 0; unsigned before positive and negative numbers preceded by a minus sign; the space filled with 0
- width optional, possession of width
- .precision optional decimal places reserved
- typecode Required
- s, __str__ method of obtaining the return value of the incoming object and is formatted to the specified position
- r, __repr__ method of obtaining the return value of the incoming object and is formatted to the specified position
- C, integers: a number into its corresponding unicode values, decimal range 0 <= i <= 1114111 (py27 only supports 0-255); character: adding a character to the specified position
- O, the octal notation is converted into an integer, and formats it into the designated location
- x, an integer converted into hexadecimal notation, and formats it into the designated location
- d, convert integer, floating point decimal representation and formats it to a designated position
- E, integer, floating-point numbers into scientific notation, and formats it into the designated location (lowercase e)
- E, converting the integer, floating-point number in scientific notation, and formats it to the specified position (uppercase E)
- F, converting integers, floating point numbers to floating point representation and formats it to the specified position (after the default 6 decimal places)
- F, ibid.
- g, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
- G, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
- %, When the flag is present formatted string, a percent sign indicates required by %%
Note: Python the percent format is automatically converted into a binary representation of an integer as not exist
(Name) Alternatively, for selecting a specified key
a = "%(name)s-----%(age)d "%{'name':'xx','age':20} print(a)
Results of the:
xx-----20
flags optional, alternative values are:
- + Right aligned; added just before the positive, negative numbers preceded by a minus sign;
- - Left; unsigned positive front, a minus sign before the negative;
- Right-aligned spaces; spaces, a minus sign before the negative before positive number;
- Align Right 0; unsigned before positive and negative numbers preceded by a minus sign; the space filled with 0
width optional, possession of width
account name 10, +, right justified, age accounted for 10, -, Left
b = "%(name)+10s————————%(age)-10d————————"%{'name':'xx','age':20} print(b)
Results of the:
xx————————20 ————————
Space, right-aligned
0, the space is filled with 0
c = "------%(year) d******%(age)010d "%{'year':2016,'age':-20} print(c)
Results of the:
------ 2016******-000000020
.precision optional decimal places reserved
Only two decimal places
d = '--------%(p).2f'%{'p':1.23456} d1 = '--------%(p)f'%{'p':1.23456} print(d) print(d1)
Results of the:
--------1.23 --------1.234560
typecode Required
- C, integers: a number into its corresponding unicode values, decimal range 0 <= i <= 1114111 (py27 only supports 0-255); character: adding a character to the specified position
- O, the octal notation is converted into an integer, and formats it into the designated location
- x, an integer converted into hexadecimal notation, and formats it into the designated location
e = 'c *** ***%%% *** the x'% (65,15,15) print (e)
Results of the:
***A***17***f
- E, integer, floating-point numbers into scientific notation, and formats it into the designated location (lowercase e)
- E, converting the integer, floating-point number in scientific notation, and formats it to the specified position (uppercase E)
f = '-----% (num) ------% of (whether) E'% { 'or' 1000000000} print(f)
Results of the:
-----1.000000e+09------1.000000E+09
- g, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
- G, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
g '-----% (num) g ------% (num1) G'% { 'whether', 1000000000, 'num1': 100} print(g)
Results of the:
-----1e+09------100
- %, When the format string flag is present, it is necessary %% represented by a percent sign (similar to turn back effect)
s = 'aaa %' print(s) s1 = 'aaa %s %%'%('bbb') print(s1)
Results of the:
aaa % aaa bbb%
format mode
Defined digital format to: Start Number '. I met ':' characters know you want to define a number of display formats. Defined as sequential format
[[fill]align][sign][#][0][width][,][.precision][type]
- [Optional] fill the space filled with character
- [optional] align alignment (in conjunction with the use of width)
- <, Left-aligned content
- > Contents in the right alignment (default)
- = Contents in the right alignment, the symbol on the left pad characters, and only valid digital type. Even: digital sign + filler +
- ^, Centered content
- sign [optional] whether the symbol numbers
- +, Positive plus plus plus minus minus sign;
- - a positive sign change, plus minus negative sign;
- Plus negative space, plus space, negative sign;
- # [Optional] For binary, octal, hexadecimal, if coupled with # displayed 0b / 0o / 0x, or do not show
- , [Optional] is added to a digital separators, such as: 1,000,000
- [optional] format bit width occupied by the width
- .precision [optional] decimal place precision reserved
- type [optional] format type
- Incoming "string type" parameter
- s, string type data format
- Blank, unspecified type, the default is None, with s
- Incoming "integer type" parameter
- b, will be automatically converted to decimal integer of 2 hexadecimal format then
- c, will automatically convert a decimal integer to its corresponding unicode character
- d, decimal integer
- o, it will be automatically converted to decimal integer octal and format;
- x, the decimal integer automatically converted into a hexadecimal format and (lower case x)
- X, will be automatically converted to decimal integer in hexadecimal format and then (upper case X)
- Passed "or decimal floating-point type" parameter
- E, is converted to scientific notation (lowercase e) shows, then format;
- E, is converted to scientific notation (uppercase E), and format;
- F, is converted to floating point (the default after the decimal point 6) shows, then format;
- F., Is converted to floating point (the default after the decimal point 6) shows, then format;
- g, e and f are automatically switched
- G, automatically switches E and F
- %, Shows the percentage (default display 6 decimal place)
- Incoming "string type" parameter
[Optional] fill the space filled with character
[optional] align alignment (in conjunction with the use of width)
- <, Left-aligned content
- > Contents in the right alignment (default)
- = Contents in the right alignment, the symbol on the left pad characters, and only valid digital type. Even: digital sign + filler +
- ^, Centered content
[optional] format bit width occupied by the width
s1 ='---{:*^20s}----'.format('welcome') print(s1) s2 ='---{:*>20s}----'.format('welcome') print(s2) s3 ='---{:*<20s}----'.format('welcome') print(s3)
Results of the:
---******welcome*******---- ---*************welcome---- ---welcome*************----
# [Optional] For binary, octal, hexadecimal, if coupled with # displayed 0b / 0o / 0x, or do not show
- b, will be automatically converted to decimal integer of 2 hexadecimal format then
- c, will automatically convert a decimal integer to its corresponding unicode character
- d, decimal integer
- o, it will be automatically converted to decimal integer octal and format;
- x, the decimal integer automatically converted into a hexadecimal format and (lower case x)
- X, will be automatically converted to decimal integer in hexadecimal format and then (upper case X)
Three methods of representation
a1 = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{:c}".format(15, 15, 15, 15, 15, 15.87623,65) a2 = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%},{1:c}".format(15,65) A3 = "numbers: {Surely, b}, {whether: o}, {Surely, d}, {whether x}, {Surely, 10}, {num%}, {c, c}". format ( Surely = 15, C = 65) print(a1) print(a2) print(a3)
Results of the:
numbers: 1111,17,15,f,F, 1587.623000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
numbers: 1111,17,15,f,F, 1500.000000%,A
, [Optional] is added to a digital separators, such as: 1,000,000
.precision [optional] decimal place precision reserved
n = '{---} ---- :, d. size (10000000) n1 = '--- {:. 2f} ----'. format (1.2345) print(n) print(n1)
Results of the:
---10,000,000---- ---1.23----
format commonly used format
tp1 = "i am {}, age {}, {}".format("seven", 18, 'alex') tp2 = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) tp3 = "i am {0}, age {1}, really {0}".format("seven", 18) tp4 = "i am {0}, age {1}, really {0}".format(*["seven", 18]) tp5 = "i am {name}, age {age}, really {name}".format(name="seven", age=18) tp6 = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) tp7 = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) tp8 = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) tp9 = "i am {:s}, age {:d}".format(*["seven", 18]) tp10 = "i am {name:s}, age {age:d}".format(name="seven", age=18) tp11 = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) print(tp1) print(tp2) print(tp3) print(tp4) print(tp5) print(tp6) print(tp7) print(tp8) print(tp9) print(tp10) print(tp11)
Results of the:
i am seven, age 18, alex i am seven, age 18, alex i am seven, age 18, really seven i am seven, age 18, really seven i am seven, age 18, really seven i am seven, age 18, really seven i am 1, age 2, really 3 i am seven, age 18, money 88888.100000 i am seven, age 18 i am seven, age 18 i am seven, age 18