Python has two ways of formatting strings, using% or using the built-in format () function.
Format a string with%
Use% in Python to format the string, with a usage and effect similar to% in the C language. The format is:% specific conversion type %data.
The following are common types of conversions
| %s |
String |
| %d |
Decimal integer |
| %x |
hexadecimal integer |
| %o |
Eight-binary integers |
| %f |
Decimal floating-point number |
| %e |
Scientific notation means floating-point numbers |
| %g |
Floating-point number represented by decimal or scientific notation |
| %% |
% itself |
Use the% format example, as follows
1>>> n = 522>>> f = 72.083>>> s ='This is a test string'4>>>'%s %s%s'%(n,f,s)5 6>>>Print('%s\n%s\n%s'% (n,f,s))//in%s in the way of output752872.089This isa test stringTen One>>>Print('%d\n%d'% (N,F))//in%d the way output A52 -72 - theThe string can only be in%s in the way of output - ->>>Print('%f\n%f'% (N,F))//in%F-Mode output -52.000000 +72.080000 - + A>>>Print('%10d\n%10f\n%10s'% (n,f,s))//set minimum width to 10, default right alignment at52 -72.080000 -This isa test string - ->>>Print('%-10d\n%-10f\n%-10s'% (n,f,s))//Align Left -52 in72.080000 -This isa test string to +>>>Print('%-10.4d\n%-10.4f\n%-10.4s'% (n,f,s))//Set Decimal Precision -0052 the72.0800 *This
Format a string using the format () function
Use the built-in format () function to format the data for use with {}. Here are some examples of use.
1>>> n = 522>>> f = 72.083>>> s ='This is a test string'4>>>Print('{}\n{}\n{}'. Format (N,f,s))//The simplest way to use552672.087This isa test string8 9>>>Print('{1}\n{2}\n{0}'. Format (N,f,s))//you can set the order of the output in this way, with the default of 0 being the first position, which in turn outputs the second, third, first dataTen72.08 OneThis isa test string A52 - -//the parameter of format can be a named variable, or a dictionary form the>>>Print('{F}\n{n}\n{s}'. Format (n=52,f=72.08,s='This is a test string')) -72.08 -52 -This isa test string + ->>> Dict1 = {'N': 52,'F': 72.08,'s':'This is a test string'} +>>>Print('{0[f]}\n{0[s]}\n{0[n]}'. Format (dict1)) A72.08 atThis isa test string -52 - ->>> Dict2 = {'N2': 13,'F2': 5.08,'S2':'Hello string'} ->>>Print('{0[f]}\n{0[s]}\n{0[n]}\n{1[f2]}\n{1[n2]}\n{1[s2]}\n{2}'. Format (DICT1,DICT2,'String3')) -72.08 inThis isa test string -52 to5.08 +13 - Hello string the String3 * $//Format the outputPanax Notoginseng>>>Print('{0[f]:10.4f}\n{0[s]:10.4s}\n{0[n]:10d}\n{1[f2]}\n{1[n2]}\n{1[s2]:15s}\n{2}'. Format (DICT1,DICT2,'String3')) -72.0800 the This +52 A5.08 the13 + Hello string - String3 $ $You can use the > settings to have alignment < set left alignment, using the ^set the center to see the example below ->>>Print('{0[f]:>10.4f}\n{0[s]:>10.4s}\n{0[n]:>10d}\n{1[f2]}\n{1[n2]}\n{1[s2]:15s}\n{2}'. Format (DICT1,DICT2,'String3')) -72.0800 the This -52Wuyi5.08 the13 - Hello string Wu String3 - About>>>Print('{0[f]:^10.4f}\n{0[s]:^10.4s}\n{0[n]:^10d}\n{1[f2]}\n{1[n2]:^10d}\n{1[s2]:15s}\n{2}'. Format (DICT1,DICT2,'String3')) $72.0800 - This -52 -5.08 A13 + Hello string the String3 - $You can also set the fill character, the position of the fill character after: In the typographic character (<,>,^) before the>>>'{0:#^20s}'. Format ('Center') the '###### #center #######'
More formatted content points for format () here.
Python data formatting