python拼接变量、字符串的3种方法

第一种,加号(“+”):

print 'py'+'thon' # output python str = 'py' print str+'thon' # output python


第二种 ,空格:

只要把两个字符串(仅限字符串)放在一起,中间有空白或者没有空格,两个字符串自动拼接为一个字符串:

print 'py' 'thon' # output python


第三种 逗号(“,”)

注意:逗号能拼接字符串和变量,但是同时会加上一个空格。

str = 'hello'
print str,'world' # output 'hello world'

猜你喜欢

转载自www.cnblogs.com/agang-php/p/10232950.html