DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

format in Python (4)

Buy Me a Coffee

*Memo:

:[f][a][s][z][#][0][w][g][.p][t] can format a string with format() as shown below:

<Format a string with 'str' input by or not by 's'>:

v = 'hello world' print(v) # hello world  print('"{:.20s}"'.format(v)) print('"{:.20}"'.format(v)) print('"{:.11s}"'.format(v)) print('"{:.11}"'.format(v)) print('"{:s}"'.format(v)) print('"{:}"'.format(v)) print('"{}"'.format(v)) # "hello world"  print('"{:.9s}"'.format(v)) print('"{:.9}"'.format(v)) # "hello wor"  print('"{:.6s}"'.format(v)) print('"{:.6}"'.format(v)) # "hello "  print('"{:.2s}"'.format(v)) print('"{:.2}"'.format(v)) # "he"  print('"{:.1s}"'.format(v)) print('"{:.1}"'.format(v)) # "h"  print('"{:.0s}"'.format(v)) print('"{:.0}"'.format(v)) # "" 
Enter fullscreen mode Exit fullscreen mode
v = '123456789' print(v) # 123456789  print('"{:.15s}"'.format(v)) print('"{:.15}"'.format(v)) print('"{:.9s}"'.format(v)) print('"{:.9}"'.format(v)) print('"{:s}"'.format(v)) print('"{:}"'.format(v)) print('"{}"'.format(v)) # "123456789"  print('"{:.6s}"'.format(v)) print('"{:.6}"'.format(v)) # "123456"  print('"{:.2s}"'.format(v)) print('"{:.2}"'.format(v)) # "12"  print('"{:.1s}"'.format(v)) print('"{:.1}"'.format(v)) # "1"  print('"{:.0s}"'.format(v)) print('"{:.0}"'.format(v)) # "" 
Enter fullscreen mode Exit fullscreen mode

<Format a string with 'int' input by or not by 'd'>:

v = 123456789 print(v) # 123456789  print('"{:d}"'.format(v)) print('"{:}"'.format(v)) print('"{}"'.format(v)) # "123456789"  print('"{:,d}"'.format(v)) print('"{:,}"'.format(v)) # "123,456,789"  print('"{:_d}"'.format(v)) print('"{:_}"'.format(v)) # "123_456_789" 
Enter fullscreen mode Exit fullscreen mode

<Format a string with 'float' input by 'f' or 'F'>:

v = 1234.5555555555 # | 10 | print(v) # 1234.5555555555 # | 10 |  print('"{:.20f}"'.format(v)) print('"{:.20F}"'.format(v)) # "1234.55555555549995006004" # | 20 |  print('"{:.14f}"'.format(v)) print('"{:.14F}"'.format(v)) # "1234.55555555549995" # | 14 |  print('"{:.13f}"'.format(v)) print('"{:.13F}"'.format(v)) # "1234.5555555555000" # | 13 |  print('"{:.10f}"'.format(v)) print('"{:.10F}"'.format(v)) # "1234.5555555555" # | 10 |  print('"{:.6f}"'.format(v)) print('"{:.6F}"'.format(v)) print('"{:f}"'.format(v)) print('"{:F}"'.format(v)) # "1234.555556" # | 6 |  print('"{:.2f}"'.format(v)) print('"{:.2F}"'.format(v)) # "1234.56"  print('"{:.1f}"'.format(v)) print('"{:.1F}"'.format(v)) # "1234.6"  print('"{:.0f}"'.format(v)) print('"{:.0F}"'.format(v)) # "1235"  print('"{:#.0f}"'.format(v)) print('"{:#.0F}"'.format(v)) # "1235."  print('"{:,.20f}"'.format(v)) print('"{:,.20F}"'.format(v)) # "1,234.55555555549995006004" # | 20 |  print('"{:,f}"'.format(v)) print('"{:,F}"'.format(v)) # "1,234.555556" # | 6 |  print('"{:_.20f}"'.format(v)) print('"{:_.20F}"'.format(v)) # "1_234.55555555549995006004" # | 20 |  print('"{:_f}"'.format(v)) print('"{:_F}"'.format(v)) # "1_234.555556" # | 6 | 
Enter fullscreen mode Exit fullscreen mode
print("{:f} {:f}".format(float('nan'), float('inf'))) # nan inf  print("{:F} {:F}".format(float('nan'), float('inf'))) # NAN INF 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)