要打印字典时,可以使用以下代码示例:
my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
# 方法1:使用循环打印字典的键和值
for key, value in my_dict.items():
print(key, value)
# 方法2:使用字符串格式化打印字典的键和值
for key, value in my_dict.items():
print("{}: {}".format(key, value))
# 方法3:使用f-string打印字典的键和值(仅适用于Python 3.6及以上版本)
for key, value in my_dict.items():
print(f"{key}: {value}")
这些方法都可以打印出字典中的键和值,而不使用引号。