在我的实践中,有一种情况叫输出中出现多余的空格:
比如我想输出hello,xxx!会输出成hello,xxx !
eg:
>>> L = ['bart','lisa','adam']
>>> for i in L:
print('hello,',i,'!')
hello, bart ! #出现多余空格
hello, lisa !
hello, adam !
改进:
1、使用格式化输出:
>>> L = ['bart','lisa','adam']
>>> for i in L:
print('hello,%s!'%i)
hello,bart!
hello,lisa!
hello,adam!
2、使用连接运算符(+)
>>> L = ['bart','lisa','adam']
>>> for i in L:
print('hello,'+i+'!')
hello,bart!
hello,lisa!
hello,adam!