字典语法错误的打印项目

时间:2018-11-26 11:14:10

标签: python dictionary printing

我想这部分代码会带来麻烦。

print '_'*10
print "NY State has :" cities['NY']
print 'OR State has :' cities['OR']

我得到的错误如下。

File "ex39.py", line 25
    print "NY State has :" cities['NY']
                                ^
SyntaxError: invalid syntax

一切似乎都是正确的,那么为什么我要提这个错误,请帮助。预先感谢

2 个答案:

答案 0 :(得分:1)

用逗号分隔打印元素

示例:

print '_'*10
print "NY State has :", cities['NY']
print 'OR State has :', cities['OR']

答案 1 :(得分:0)

您还有其他选择是使用.format

print '_'*10
print "NY State has : {}".format(cities['NY'])
print 'OR State has : {}'.format(cities['OR'])