python 输出字符串拼接
时间: 2023-12-21 10:29:49 浏览: 95
在Python中,有两种常用的字符串拼接方式:使用加号(+)和使用join()方法。其中,使用join()方法拼接字符串的效率更高。
以下是使用join()方法拼接字符串的例子:
```python
strlist = ['Hello', ' ', 'World', '!']
print(''.join(strlist)) # 输出:Hello World!
```
在上面的例子中,我们首先定义了一个包含多个字符串的列表strlist,然后使用join()方法将列表中的所有字符串拼接成一个字符串,并将结果输出。
需要注意的是,join()方法的参数必须是一个可迭代对象,例如列表、元组等。此外,join()方法的调用者是一个字符串,表示用于连接可迭代对象中各个元素的分隔符。
阅读全文
相关推荐













