有人能告诉我为什么下面的两个python代码产生完全不同的结果吗?
这是我的密码:有人能告诉我为什么下面的两个python代码产生完全不同的结果吗?,python,python-3.x,syntax,Python,Python 3.x,Syntax,这是我的密码: >>> hellos = [ "hello", "bonjour", "hola"] >>> for hi in hellos:
>>> hellos = [ "hello", "bonjour", "hola"]
>>> for hi in hellos:
... print(hi + " World!")
...
hello World!
bonjour World!
hola World!
>>> hellos = [ "hello", "bonjour", "hola"]
>>> for hi in hellos:
... print("hi" + " World!")
...
hi World!
hi World!
hi World!
你只是在第二个中包含字符串
“嗨”+“世界!”=嗨世界 第二次,hi在引号中。所以它是一个字符串文本。它不是一个变量。具体来说,它不是迭代器的值 首先,你用
“世界!”
因此,输出为:
hello World!
bonjour World!
hola World!
hi World!
hi World!
hi World!
其次,您只需将“hi”
与“World!”
连接起来
因此,输出为:
hello World!
bonjour World!
hola World!
hi World!
hi World!
hi World!
请修复格式您希望输出是什么?因为当您在
“…”
中包含某个内容时,它是一个字符串-可能是一个好的开始