*Memo:
- My post explains format_map().
- My post explains Format Specification with format_map() (1).
- My post explains Format Specification with format_map() (2).
- My post explains Format Specification with format_map() (4).
- My post explains Format Specification with format_map() (5).
- My post explains f-strings.
- My post explains format().
- My post explains a string.
:[f][a][s][z][#][0][w][g][.p][t]
can format a string with format_map() as shown below:
<Format a string with 'str' input by or not by 's'>:
v = {'k':'hello world'}
print(v['k'])
# hello world
print('"{k:.20s}"'.format_map(v))
print('"{k:.20}"'.format_map(v))
print('"{k:.11s}"'.format_map(v))
print('"{k:.11}"'.format_map(v))
print('"{k:s}"'.format_map(v))
print('"{k:}"'.format_map(v))
print('"{k}"'.format_map(v))
# "hello world"
print('"{k:.9s}"'.format_map(v))
print('"{k:.9}"'.format_map(v))
# "hello wor"
print('"{k:.6s}"'.format_map(v))
print('"{k:.6}"'.format_map(v))
# "hello "
print('"{k:.2s}"'.format_map(v))
print('"{k:.2}"'.format_map(v))
# "he"
print('"{k:.1s}"'.format_map(v))
print('"{k:.1}"'.format_map(v))
# "h"
print('"{k:.0s}"'.format_map(v))
print('"{k:.0}"'.format_map(v))
# ""
v = {'k':'123456789'}
print(v['k'])
# 123456789
print('"{k:.15s}"'.format_map(v))
print('"{k:.15}"'.format_map(v))
print('"{k:.9s}"'.format_map(v))
print('"{k:.9}"'.format_map(v))
print('"{k:s}"'.format_map(v))
print('"{k:}"'.format_map(v))
print('"{k}"'.format_map(v))
# "123456789"
print('"{k:.6s}"'.format_map(v))
print('"{k:.6}"'.format_map(v))
# "123456"
print('"{k:.2s}"'.format_map(v))
print('"{k:.2}"'.format_map(v))
# "12"
print('"{k:.1s}"'.format_map(v))
print('"{k:.1}"'.format_map(v))
# "1"
print('"{k:.0s}"'.format_map(v))
print('"{k:.0}"'.format_map(v))
# ""
<Format a string with 'int' input by or not by 'd'>:
v = {'k':123456789}
print(v['k'])
# 123456789
print('"{k:d}"'.format_map(v))
print('"{k:}"'.format_map(v))
print('"{k}"'.format_map(v))
# "123456789"
print('"{k:,d}"'.format_map(v))
print('"{k:,}"'.format_map(v))
# "123,456,789"
print('"{k:_d}"'.format_map(v))
print('"{k:_}"'.format_map(v))
# "123_456_789"
<Format a string with 'float' input by 'f' or 'F'>:
v = {'k':1234.5555555555}
# | 10 |
print(v['k'])
# 1234.5555555555
# | 10 |
print('"{k:.20f}"'.format_map(v))
print('"{k:.20F}"'.format_map(v))
# "1234.55555555549995006004"
# | 20 |
print('"{k:.14f}"'.format_map(v))
print('"{k:.14F}"'.format_map(v))
# "1234.55555555549995"
# | 14 |
print('"{k:.13f}"'.format_map(v))
print('"{k:.13F}"'.format_map(v))
# "1234.5555555555000"
# | 13 |
print('"{k:.10f}"'.format_map(v))
print('"{k:.10F}"'.format_map(v))
# "1234.5555555555"
# | 10 |
print('"{k:.6f}"'.format_map(v))
print('"{k:.6F}"'.format_map(v))
print('"{k:f}"'.format_map(v))
print('"{k:F}"'.format_map(v))
# "1234.555556"
# | 6 |
print('"{k:.2f}"'.format_map(v))
print('"{k:.2F}"'.format_map(v))
# "1234.56"
print('"{k:.1f}"'.format_map(v))
print('"{k:.1F}"'.format_map(v))
# "1234.6"
print('"{k:.0f}"'.format_map(v))
print('"{k:.0F}"'.format_map(v))
# "1235"
print('"{k:#.0f}"'.format_map(v))
print('"{k:#.0F}"'.format_map(v))
# "1235."
print('"{k:,.20f}"'.format_map(v))
print('"{k:,.20F}"'.format_map(v))
# "1,234.55555555549995006004"
# | 20 |
print('"{k:,f}"'.format_map(v))
print('"{k:,F}"'.format_map(v))
# "1,234.555556"
# | 6 |
print('"{k:_.20f}"'.format_map(v))
print('"{k:_.20F}"'.format_map(v))
# "1_234.55555555549995006004"
# | 20 |
print('"{k:_f}"'.format_map(v))
print('"{k:_F}"'.format_map(v))
# "1_234.555556"
# | 6 |
print("{k1:f} {k2:f}".format_map({'k1':float('nan'), 'k2':float('inf')}))
# nan inf
print("{k1:F} {k2:F}".format_map({'k1':float('nan'), 'k2':float('inf')}))
# NAN INF
Top comments (0)