我在python中有一个字典如下:
dict1 = {'a':1, 'b':2, 'c':3}
到目前为止,我在Python 3中运行了以下代码:
for key, value in dict1.items() :
print (key* value,end='')
这给了我输出:cccbba
我的问题是如何打印 -
Your answer : cccba.
答案 0 :(得分:1)
这样的事情:
dict1 = {'a':1, 'b':2, 'c':3}
print ("Your answer : ",end='')
for key, value in dict1.items() :
print (key* value,end='')
答案 1 :(得分:1)
你应该真正掌握str.format
:
>>> print("Your answer: {0}".format("".join([k * v for k, v
in dict1.items()])))
Your answer: acccbb