Format/printf, pythonprintf
For Python string formatting, use "character % format 1% Format 2 characters" % (variable 1, variable 2), and % format to accept the type of the variable. A simple example is as follows:
# Example: String formatting Name = '17jo 'print 'www. % s.com '% Name> www.17jo. comName = '17jo 'Zone = 'com 'print' www. % s. % s' % (Name, Zone)> www.17jo.com
When a string is formatted, the hundred semicolons are followed by different format symbols, representing different types of conversion. The specific symbols are shown below.
Format Symbol: Type % s string % d/% I decimal integer % u decimal integer % o octal integer % x/% X hexadecimal integer % e/% esubject count % f /% F Floating Point Number % output %
When the format symbol is a number, you can add a number and a fill space, for example, % [0] [total digits] [.] [number of decimal places] to set the style to be converted. The usage is as follows:
# Example: number formatting nYear = 2018 nMonth = 8 nDay = 18 # formatting date % 02d number converted to two integer without entering 0 print '% 04d-% 02d-% 02d' % (nYear, nMonth, nDay)> # output result fValue = 8.123 print '% 06.2f' % fValue # reserved 2-digit floating point type with a width of 6> 008.12 # output print '% d' % 10 # output decimal>> 10 print '% o' % 10 # output eight bytes> 12 print' % 02x' % 10 # output two hexadecimal values, lowercase letter vacancy completion> 0 aprint '% 04X' % 10 # output 4-digit hexadecimal notation, uppercase letter vacancy completion> 000 Aprint '%. 2e' % 1.2888 # use scientific notation to output floating-point type to retain 2 decimal places> 1.29e + 00
That which didn't kill me makes me stronger