python -- String formatting

python's string formatting has two ways: percentile and format. Percentage is relatively old, while format is more advanced.

1) Percentage format

Syntax:
  %[(name)][flags][width].[precision]typecode

name:--optional, used to select the specified key
flags: - Optional. Optional values are:
+:--Right alignment, plus a positive sign before a positive number, plus a negative sign before a negative number
-:--Left alignment, no sign before positive number, plus minus sign before negative number
Spaces: - Right alignment, plus spaces before positive numbers and minus signs before negative numbers
0: - Right alignment, no sign before positive number, plus minus sign before negative number, fill in the blank with 0
width:--Optional, width-occupying
precision:--Optional number of digits reserved after decimal point
typecode:--Required. Values available are:
% s string (displayed with str())
% r string (displayed with repr())
% c Single character
% b binary integer
% d decimal integer
% i decimal integer
% o octal integer
% x hexadecimal integer
% e index (base written as e)
% E index (base written as E)
% f Floating Point
% F floating point number, same as above
% g index (e) or floating point number (depending on display length)
% G Index (E) or Floating Point (by Display Length)
%% Represents a character "%"

Note: Percentage formatting in python does not automatically convert integers into binary representations

Common formatting examples:

 1 str1 = "I am learning %s" % "python"
 2 
 3 str2 = "my name is %s , age %d" %("yusheng_liang", 20)
 4 
 5 str3 = "my name is %(name)s , age %(age)d" %{"name": "yusheng_liang", "age": 20}
 6 
 7 str4 = "precent %.2f" % 99.97654
 8 
 9 str5 = "I am %(qq).2f" %{"qq": 123.45678}
10 
11 str6 = "I am %(qq).2f %%" %{"qq": 123.45678}
12 
13 
14 print("str1:", str1)
15 print("str2:", str2)
16 print("str3:", str3)
17 print("str4:", str4)
18 print("str5:", str5)
19 print("str6:", str6)
View Code

Example results:

 

 

2)format mode

Syntax:
  [[fill]align][sign][#][0][width][,][.precision][type]

fill:-- Optional characters filled in blanks
Alignment: - Optional, align ment (with width)
Left alignment of content
> Right alignment of content (default)
= Right-aligned content, placing symbols on the left side of the padding character, and valid only for numeric types. Namely: Symbol + Filler + Number
^ Content in the middle
sign:--Optional, with or without symbolic numbers
+ Positive plus positive, negative plus negative
- Positive sign unchanged, negative sign plus negative sign
Spaces: Positive and negative spaces
# Optionally, for binary, octal and hexadecimal systems, if# is added, 0b/0o/0x will be displayed, otherwise it will not be displayed.
:-- Optional, as a numeric divider, e.g. 1,000,000
Width: - Optional, formatting bit width
precision: - Optional, decimal reservation accuracy
Type:--Optional, formatted type
For integer types, there are six output formats:
b: Binary mode of output integer;
c: Output Unicode characters corresponding to integers;
d: decimal mode of output integers;
o: Octal mode of output integer;
x: The lowercase hexadecimal mode of output integers;
X: Uppercase hexadecimal mode for output integers;

For floating-point type, the output format includes four types:
e: The exponential form of the lowercase letter e corresponding to the output floating point number;
E: The exponential form of the capital letter E corresponding to the output floating point number;

f: Standard floating-point form for output floating-point numbers;

% Output Floating Point Percentage Form.

Common formatting examples:

 1 str1 = "I am {}, age {}".format("yusheng_liang", 20)
 2 str2 = "I am {}, age {}".format(*["yusheng_liang", 20])
 3 str3 = "I am {0}, age {1}".format("yusheng_liang", 20)
 4 str4 = "I am {name}, age {age}".format(name = "yusheng_liang", age = 20)
 5 str5 = "I am {name}, age {age}".format(**{"name": "yusheng_liang", "age": 20})  #Providing data in a dictionary
 6 str6 = "I am {0[0]}, age {0[1]}, set {1[2]}".format([1, 2, 3], [11, 22, 33])
 7 str7 = "I am {:s}, age {:d}, money {:f}".format("yusheng_liang", 20, 5000.23)
 8 str8 = "I am {:s}, age {:d}".format("yusheng_liang", 20)
 9 str9 = "I am {name:s}, age {age:d}".format(name = "yusheng_liang", age = 20)
10 str10 = "I am {name:s}, age {age:d}".format(**{"name": "yusheng_liang", "age": 20})
11 str11 = "number: {:b}, {:o}, {:d}, {:x}, {:X}, {:%}".format(15, 15, 15, 15, 15, 15, 15.456, 2)
12 
13 
14 print("str1:", str1)
15 print("str2:", str2)
16 print("str3:", str3)
17 print("str4:", str4)
18 print("str5:", str5)
19 print("str6:", str6)
20 print("str7:", str7)
21 print("str8:", str8)
22 print("str9:", str9)
23 print("str10:", str10)
24 print("str11:", str11)
View Code

Example results:

Keywords: Python

Added by vandalite on Mon, 07 Oct 2019 00:19:13 +0300