Python 从字典打印元素产生错误
Python Printing Elements from a dictionary produces error
我为使用循环读取的用户名创建了这段代码。
users = {
'aeinstein': {
'first':'albert',
'last':'einstein',
'location':'princeton'
},
'mcurie': {
'first':'marie',
'last':'curie',
'location':'paris',
}
}
for username, user_info in users.items():
print("\nUsername: " + username)
full_name = user_info['first'], user_info['last']
location = user_info['location']
print("\tFull name:" + full_name.title())
print("\tLocation:" + location.title())
现在,如果您在 for 循环中观察到以下行
full_name = user_info['first'], user_info['last']
我希望1 附加值 albert einstein
和 marie curie
,但这会产生错误
print("\tFull name:" + full_name.title())
AttributeError: 'tuple' object has no attribute 'title'
但是为什么我的方法是错误的,而下面的方法是正确的...
full_name = user_info['first'] + " " + user_info['last']
产生以下结果
Username: aeinstein
Full name:Albert Einstein
Location:Princeton
Username: mcurie
Full name:Marie Curie
Location:Paris
1来自评论: 所以当你说 print("hello", "world")
这种类型的字符串连接是正确的,但不是我展示的例子?
通过在 user_info['first'], user_info['last']
中添加 ,
运算符,您告诉 Python 您正在给它一个包含两个字符串的元组。通过使用 +
运算符,您只是将两个字符串连接成一个字符串。
表达式 user_info['first'], user_info['last']
创建一个包含两个元素的元组(在本例中,元素是字符串)。元组 object 没有 title
方法,但是如果你像 user_info['first'] + " " + user_info['last']
那样用加号运算符连接,你会创建一个字符串而不是元组,所以你可以使用标题方法
full_name = user_info['first'], user_info['last']
I expect this to append the value albert einstein
and marie curie
[…]
你的期望是错误的。
but why is my method wrong and the following therefore correct...
full_name = user_info['first'] + " " + user_info['last']
因为+
是字符串的连接运算符,而,
不是。
正如其他几个人的回答,您需要使用
full_name = user_info['first']+" "+ user_info['last']
或
full_name = "%s %s" %(user_info['first'],user_info['last'])
我为使用循环读取的用户名创建了这段代码。
users = {
'aeinstein': {
'first':'albert',
'last':'einstein',
'location':'princeton'
},
'mcurie': {
'first':'marie',
'last':'curie',
'location':'paris',
}
}
for username, user_info in users.items():
print("\nUsername: " + username)
full_name = user_info['first'], user_info['last']
location = user_info['location']
print("\tFull name:" + full_name.title())
print("\tLocation:" + location.title())
现在,如果您在 for 循环中观察到以下行
full_name = user_info['first'], user_info['last']
我希望1 附加值 albert einstein
和 marie curie
,但这会产生错误
print("\tFull name:" + full_name.title())
AttributeError: 'tuple' object has no attribute 'title'
但是为什么我的方法是错误的,而下面的方法是正确的...
full_name = user_info['first'] + " " + user_info['last']
产生以下结果
Username: aeinstein
Full name:Albert Einstein
Location:Princeton
Username: mcurie
Full name:Marie Curie
Location:Paris
1来自评论: 所以当你说 print("hello", "world")
这种类型的字符串连接是正确的,但不是我展示的例子?
通过在 user_info['first'], user_info['last']
中添加 ,
运算符,您告诉 Python 您正在给它一个包含两个字符串的元组。通过使用 +
运算符,您只是将两个字符串连接成一个字符串。
表达式 user_info['first'], user_info['last']
创建一个包含两个元素的元组(在本例中,元素是字符串)。元组 object 没有 title
方法,但是如果你像 user_info['first'] + " " + user_info['last']
那样用加号运算符连接,你会创建一个字符串而不是元组,所以你可以使用标题方法
full_name = user_info['first'], user_info['last']
I expect this to append the value
albert einstein
andmarie curie
[…]
你的期望是错误的。
but why is my method wrong and the following therefore correct...
full_name = user_info['first'] + " " + user_info['last']
因为+
是字符串的连接运算符,而,
不是。
正如其他几个人的回答,您需要使用
full_name = user_info['first']+" "+ user_info['last']
或
full_name = "%s %s" %(user_info['first'],user_info['last'])