这个程序才是对的:
# -*- coding: utf-8 -*- L = ['Bart', 'Lisa', 'Adam'] for name in L: print("Hello,%s!" % name)
输出结果:
Hello,Bart! Hello,Lisa! Hello,Adam!
Hello,Bart!
Hello,Lisa!
Hello,Adam!
下面这种方法其实是错误的:
for name in L: print('hello,',name,'!')
因为输入结果:
hello, Bart ! hello, Lisa ! hello, Adam !
hello, Bart !
hello, Lisa !
hello, Adam !
因为逗号,在做拼接时会自动插入一个空格,所以不能用拼接的方法,应该使用格式化输出。 "hello,%s!" % name
,
"hello,%s!" % name
用这个‘+’号就了呢。。 for name in L: print('hello,'+name+'!')
Sign in to make a reply
土城剑客
这个程序才是对的:
输出结果:
Hello,Bart!
Hello,Lisa!
Hello,Adam!
下面这种方法其实是错误的:
因为输入结果:
hello, Bart !
hello, Lisa !
hello, Adam !
因为逗号
,
在做拼接时会自动插入一个空格,所以不能用拼接的方法,应该使用格式化输出。"hello,%s!" % name