*Memos:
- My post explains Format Specification with format() (1).
- My post explains Format Specification with format() (2).
- My post explains Format Specification with format() (3).
- My post explains Format Specification with format() (4).
- My post explains Format Specification with format() (5).
- My post explains format().
- My post explains f-strings.
- My post explains a string.
format_map() can format and create a string with a dictionary as shown below. *Format Specification can be used with format_map()
:
*Memos:
- The 1st argument is
mapping
(Required-Type:dict
). *Don't usemapping=
.
<{key}>:
*Memos:
- A
{key}
can output a value with no limit. -
{key}
doesn't accept any spaces. -
{}
cannot be used.
v = {'name':'John', 'age':36}
print('- {name} - {age} -'.format_map(v))
# - John - 36 -
print('- {name} - {age} - {name} - {age} -'.format_map(v))
# - John - 36 - John - 36 -
print('{ name }'.format_map(v))
# KeyError: ' name '
print('{ age }'.format_map(v))
# KeyError: ' age '
print('- {} - {} '.format_map(v))
# ValueError: Format string contains positional fields
*Format Specification can be used with format_map()
as I explain it with f-strings in my post:
v = {'k':-0.0}
print('"{k:0>-z#010_.0f}"'.format_map(v))
print('"{k:0>-z#10_.0f}"'.format_map(v))
print('"{k:>-z#010_.0f}"'.format_map(v))
# "000000000."
print('"{k:0>-z#010_.0f}" "{k:0>-z#10_.0f}" "{k:>-z#010_.0f}"'.format_map(v))
# "000000000." "000000000." "000000000."
<key[key]>:
*key cannot have ""
or ''
.
v = {'name':{'fname':'John', 'lname':'Smith'}}
print('- {name}\n- {name[fname]} - {name[lname]} -'.format_map(v))
# - {'fname': 'John', 'lname': 'Smith'}
# - John - Smith -
print('{name["fname"]}'.format_map(v))
# KeyError: '"fname"'
print("{name['lname']}".format_map(v))
# KeyError: "'lname'"
v1 = {'person':{'name':{'fname':'John', 'lname':'Smith'}}}
v2 = '- {person}\n- {person[name]}\n\
- {person[name][fname]} - {person[name][lname]} -'
print(v2.format_map(v1))
# - {'name': {'fname': 'John', 'lname': 'Smith'}}
# - {'fname': 'John', 'lname': 'Smith'}
# - John - Smith -
<key[index]>:
v = {'name':['John', 'Smith']}
print('- {name}\n- {name[0]} - {name[1]} -'.format_map(v))
# - ['John', 'Smith']
# - John - Smith -
v1 = {'name':[['John', 'Smith']]}
v2 = '- {name}\n- {name[0]}\n- {name[0][0]} - {name[0][1]} -'
print(v2.format_map(v1))
# - [['John', 'Smith']]
# - ['John', 'Smith']
# - John - Smith -
<key.key>:
*key
cannot be called with ()
.
import math
v = {'m':math}
print('{m.pi}'.format_map(v))
# 3.141592653589793
print('{m.pow}'.format_map(v))
# <built-in function pow>
print('{m.pow(3, 2)}'.format_map(v))
# AttributeError: module 'math' has no attribute 'pow(3, 2)'
<!s & !r & !a>:
*Memos:
-
!s
is str() which can create a string. -
!r
is repr() which can create a printable string. -
!a
is ascii() which can create the printable string of ASCII characters. *Non-ASCII characters are represented with\x
,\u
, or\U
escape.
v = {"key":"Jφhи\tSмiтh"}
print("{key!s}".format_map(v))
# Jφhи Sмiтh
print("{key!r}".format_map(v))
# 'Jφhи\tSмiтh'
print("{key!a}".format_map(v))
# 'J\u03c6h\u0438\tS\u043ci\u0442h'
v = {'key':'I\'m Jφhи\tSмiтh.'}
print('{key!s}'.format_map(v))
# I'm Jφhи Sмiтh.
print('{key!r}'.format_map(v))
# "I'm Jφhи\tSмiтh."
print('{key!a}'.format_map(v))
# "I'm J\u03c6h\u0438\tS\u043ci\u0442h."
<{{ & }}>
*Memos:
-
{
can escape{
. -
}
can escape}
.
v = {}
print('The bracket {{ is escaped.'.format_map(v))
# The bracket { is escaped.
print('The bracket }} is escaped.'.format_map(v))
# The bracket } is escaped.
print('The {{brackets}} are escaped.'.format_map(v))
# The {brackets} are escaped.
Top comments (0)