Tags
Asked 3 years ago
3 Aug 2021
Views 4501
steave ray

steave ray posted

TypeError: format() argument after ** must be a mapping, not str in Python


print('{0} {1} {0!r}'.format(**'abrakadabara')) 

i am formating string but i prompted with this error

TypeError: format() argument after ** must be a mapping, not str


steave ray

steave ray
answered Aug 3 '21 00:00


you should use * instead of ** at format function , see below code :

print('{0} {1} {0!r}'.format(*'abrakadabara')) 


will print

a b 'a'


so if suppose if you have

abc = {'a': 'A', 'b': 'B'}
print('{a} {b} {a!r}'.format(**abc)) 

in above code with ** , can Access arguments’ attributes which is a , b in our case so result is


A B 'A'



Post Answer