Why is print (* * data) reporting an error instead of displaying the appearance after unpacking

data = {‘aaa’:‘a’,‘bbb’:‘b’}
str1 = ‘{aaa} = {bbb}’.format(**data)

Traceback (most recent call last):
File “D:\pythonProject4\20231030.py”, line 29, in
TypeError: ‘aaa’ is an invalid keyword argument for print()

What should I write if I want to look at the unpacked appearance in the dict?


What are you hoping to see?

print(**data) is telling the interpreter “call the print function and give it keyword arguments from this dictionary.” It translates to print(aaa='b', bbb='b') which is why you get the error you see: print does not have a keyword argument aaa.

If you just print(data) you’ll get a representation of the dictionary, which will look a lot like the code you used to create it.

What I want to see is print (aaa='b ', bbb=‘b’)


you can use comprehension to format a list of strings, then join and print
print(','.join([f"{k}='{v}'" for k,v in data.items()]))
will print

If you do just print(data), exactly what is wrong about the result?