Python中连接字符串有两种常用方法。一种是通过str1+str2来实现连接,另一种是通过join方法来实现。代码实现如下:
str1='hello'
str2='world'
res1=str1+str2
print(res1)
#helloworld
res2=''.join([str1,str2])
print(res2)
#helloworld
注意:join方法的参数要求是列表形式的,将要合并的多个元素用’‘所引的字符串连接;str的join方法可以修改连接字符串,更加灵活
str1='hello'
str2='world'
res2=' '.join([str1,str2])
print(res2)
#hello world