*Memo:
- 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() (3).
- 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.
format_map() can create a string with a dictionary, then format the string as shown below:
*Memo:
- The 1st argument is
mapping
(Required-Type:Mapping):- Don't use
mapping=
.
- Don't use
<{key}>:
*Memo:
- 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()
:
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]>:
*Memo:
- 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>:
*Memo:
-
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>:
*Memo:
-
!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."
<{{ & }}>
*Memo:
-
{
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)