首页 > 解决方案 > attribute error when using format - python

问题描述

D = {'z': {'A': 'Hello', 'B': 'World'}}
'{z.get("A")} {z.get("B")}'.format(**D)

*** AttributeError: 'dict' object has no attribute 'get("A")'

Why am I getting this error, and how should I fix it?

I'd like to print "Hello World"

标签: pythondictionary

解决方案


您可以在python3

D = {'z': {'A': 'Hello', 'B': 'World'}}
print(f"{D['z'].get('A')} {D['z'].get('B')}")

或者你可以试试这个

print(f"{D['z']['A']} {D['z']['B']}")

输出:

Hello World

推荐阅读