在python字典中操作和打印项目

use*_*071 1 python python-2.7

我有以下代码:

ex_dict={1:"how",3:"do you",7:"dotoday"}
for key in ex_dict:
    string= key," and this is ", ex_dict[key]
    print (string)
Run Code Online (Sandbox Code Playgroud)

输出是:

(1, ' and this is ', 'how')
(3, ' and this is ', 'do you')
(7, ' and this is ', 'dotoday')
Run Code Online (Sandbox Code Playgroud)

我的预期输出:

1 and this is how
3 and this is do you
7 and this is dotoday
Run Code Online (Sandbox Code Playgroud)

我似乎无法弄清楚如何摆脱输出中的字典格式.

And*_*bis 5

只是+用来组合字符串:

string = str(key) + " and this is " + ex_dict[key]
Run Code Online (Sandbox Code Playgroud)

由于key是一个整数,并且您只能使用+运算符连接字符串,因此您也应该转换key为字符串.