[Output Control] Python clever but useless in the format

Front

Environment: Python3.6.5
Discussion Point: output print, string format control, control%




print basic control

Simple demonstration:

a = 1
b = '@Hello yanshanbei!'
print('默认换行')
print('不换行,end 可以调整间距',end = '')
print()
print(a,b)#默认一个空格分隔

Output:

format control

  1. str present in the format, i.e. is str.format ()
  2. format may be controlled: the digital footprint length, alignment, padding character, floating point precision
  3. {} Rely on embedding:
    3.1 according to the filling sequence {} ships
    3.2 If {1} {0} filled sequentially according args, by filling the same way using the variable keyword location
    3.3 format also supports, unpack filled, tuple or list using the * , using dict ** (wherein, after filling dict unpacked by keyword position)
  4. Other controls} {
    4.1 digital footprint length
    4.2 float-precision
    4.3 fill characters
    4.4 Alignment
    ......

point 3:

point 4:

a = 1
b = 12.123456
print('{:.2f}'.format(a))#保留2位小数
print('{:5}'.format(a))  #占5位
print('{:>5}'.format(a)) #右对齐
print('{:<5}'.format(a)) #左对齐
print('{:^5}'.format(a)) #居中对齐
print('{:*^5}'.format(a))#居中对齐,空白填充 *

Output:

There are many practical skills, self-interest can be viewed in the official documents;


% Control

This usage is similar to the% C, except that, after the multi-parameter is required in the form% (a, b) of

print('I Love %s'%'China') #%s 字符串
print('I Love %d'%18)      #%d 整数
a = 12.345
b = 'Hello'
print('[12.345] = %f'%a)   #默认保留6位小数
print('[12.345] = %f,b = %s'%(a,b))
#……

Output:





Finally, I wish you success!

Guess you like

Origin www.cnblogs.com/yanshanbei/p/11997099.html