Python methods for formatting output strings
- 2021-01-18 06:31:05
- OfStack
python format strings have two string format controls: % and {}.
String input data format type (% format manipulation symbol)
%% | 百分号标记#就是输出1个% |
%c | 字符及其ASCII码 |
%s | 字符串 |
%d | 有符号整数(10进制) |
%u | 无符号整数(10进制) |
%o | 无符号整数(8进制) |
%x | 无符号整数(106进制) |
%X | 无符号整数(106进制大写字符) |
%e | 浮点数字(科学计数法) |
%E | 浮点数字(科学计数法,用E代替e) |
%f | 浮点数字(用小数点符号) |
%g | 浮点数字(根据值的大小采用%e或%f) |
%G | 浮点数字(类似于%g) |
%p | 指针(用106进制打印值的内存地址) |
%n | 存储输出字符的数量放进参数列表的下1个变量中 |
String format control %[(name)][flag][width][.][precision]type
name: NULL, number (placeholder), name (pass parameter name, cannot start with a number) formatted in dictionary format mapping, which is the key name
flag: Markup format qualifies symbol, including +-# and 0,+ for right alignment (plus and minus signs will be displayed),- left alignment, default padding space (that is, default right-alignment), 0 for padding 0, # for 0, hexadecimal padding 0x for ocimal padding, 0b for binary padding
width: Width (Minimum length, including decimal points, padded if less than width)
precision: The number of decimal places, the same as C
type: Input format type, see above
One kind of format_spec format {[name] [:] [[fill] align] [sign] [#] [0] [width] [of] [. precision] [type]}
with
{}
The package name name is passed to
format
Named = value, non dictionary mapping, the same as above
fill = < any character > #fill means you can fill in any character
align = " < " | " > = "|" "|" ^ "# align is alignment, < It's left aligned, > It's right-aligned, and the ^ is center-aligned.
sign = "+" | "" |" "# sign is symbol, plus + said, - said the minus sign
width = integer#width is the number width, indicating the total number of digits output
precision = integer#precision is the decimal reserved digit
c type = "b" | "" |" d "|" e "|" E "|" f "|" F "|" g "|" G "|" n "|" o "|" s "|" x "|" X "|" % "# type is output numeric value representation, b, for example, is a binary representation; For example, E is exponential; For example, X is in base 106
Example (local test run environment: Python3.6)
>>> print("{:,}".format(123456))# The output 1234,56
123,456
>>> print("{a:w^8}".format(a="8"))# The output www8wwww, fill w
www8wwww
>>> print("%.5f" %5)# The output 5.000000
5.00000
>>> print("%-7s3" %("python"))# The output python 3
python 3
>>> print("%.3e" %2016)# The output 2.016e+03, I could also write it big E
2.016e+03
>>> print("%d %s" %(123456,"jb51"))# The output 123456 jb51
123456 jb51
>>> print("%(what)s is %(year)d" % {"what":"this year","year":2016})# The output this year is 2016
this year is 2016
>>> print("{0}{1}".format("hello","fun"))# The output hellofun, This has to do with CSharp Formatting characters of ( A placeholder ) similar
hellofun
>>> print("{}{}{}".format("jb51",".","net"))# The output ofstack.com
ofstack.com
>>> print("{a[0]}{a[1]}{a[2]}".format(a=["jb51",".","net"]))# The output ofstack.com
ofstack.com
>>> print("{dict[host]}{dict[dot]}{dict[domain]}".format(dict={"host":"www","domain":"ofstack.com","dot":"."}))# The output www.ofstack.com
www.ofstack.com
>>> print("{a}{b}".format(a="python",b="3"))# The output python3
python3
>>> print("{who} {doing} {0}".format("python",doing="like",who="I"))# The output I like python
I like python
>>>
P.S. About Python
format
Function format output operations refer to the previous ES106en string basic operations