There are a lot of symbols in string formatting, and this article describes them in detail so that they can be found at any time.
%s :String formatting, which is also the most commonly used
%d :Formatting integers, also more commonly used.
%c :Formatting characters and ASCII codes
%u :format unsigned integers
%f :Format floating-point numbers; you can specify the precision after decimals.
%e :Format floating point numbers using scientific notation
%o :format unsigned octal numbers
%x :format unsigned hexadecimal number
%p :Hexadecimal number formatted variable address
%g :short for %e and %f
Demonstrate the effect of each placeholder formatting when it is used
'''Formatting strings''' var_s = '%s' % 'I am a string' print(var_s) '''Formatting integers''' var_d = '%d' % 10 print(var_d) '''Formatting characters''' var_c = '%c' % "w" print(var_c) # Formatting characters, here it must be a character not a string, otherwise the following error occurs. var_c = '%c' % "water" print(var_c) # var_c = '%c' % "water" # TypeError: %c requires int or char '''Formatting unsigned integers''' var_u = '%u' % 123 print(var_u) '''Formatting Floating Point Numbers''' var_f = '%.2f' % 2.3 print(var_f) '''Formatting floating point numbers in scientific notation''' var_e = '%e' % 2.3333333 print(var_e) '''Formatting unsigned octal numbers''' var_o = '%o' % 100 print(var_o) # And the result is 144 # You can use the int function to convert the resulting octal to decimal again the result is 100, which is the same as we expected. print(int('144',8)) '''Formatting unsigned hexadecimal numbers''' var_x = '%x' % 100 print(var_x) # And you get a result of 64 print(int('64',16)) # The same way, converting it back results in 100 # '''Address of hexadecimal formatted variable''' var_p = '%p' % '144' print(var_p) # ValueError: unsupported format character 'p' (0x70) at index 1 # Execute the formatting of %p and find it is not supported, the first time I use it I was surprised that it is not supported. '''Abbreviation of %e and %f''' var_g = '%.3g' % 2.12 print(var_g)
Specify placeholder width
>>> print ("Name : %10s, Age : %9d, Height: %8.2f"%("tom", 37, 2.29)) Name : tom, Age : 37, Height : 2.29
The formatting is a little hard to read, so here's how we're going to align it.
Specify Placeholder Width - Left Aligned
>>> print ("Name : %-10s, Age : %-9d, Height : %-8.2f"%("tom", 37, 2.29)) Name : tom , Age : 37 , Height : 2.29 note:Formatting Optional Markers, Optional parameters + right-aligned;positive number preceded by exactly,negative number preceded by a minus sign; - left justification;positive number preceded by an unsigned (i.e. negative or zero),negative number preceded by a minus sign; blank space right-aligned;正数前加blank space,negative number preceded by a minus sign; 0 right-aligned;positive number preceded by an unsigned (i.e. negative or zero),negative number preceded by a minus sign;expense or outlay0Fill in the blanks as if: -10s, indicate10String Position, left justification,后面expense or outlayblank space填充
Specify placeholders - use 0 as a placeholder
>>> print ("Name : %-10s, Age : %09d, Height : %08.2f"%("tom", 37, 2.29)) Name : tom , Age : 000000037, Height : 00002.29
summarize
The use of placeholders is very common, but also very practical, it can be very good to help us solve some of the string formatting problems, transformed into a variety of uses, some commonly used placeholders can be remembered in order to improve programming efficiency.
to this article on python in the top ten % placeholder corresponds to the use of the formatting of the article is introduced to this, more related to python placeholder formatting content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!