Python 官方文档:入门教程 => 点击学习
可以使用strip()方法来去除字符串首尾的空格。示例代码如下:```pythonstring = " hello world
可以使用strip()方法来去除字符串首尾的空格。
示例代码如下:
```python
string = " hello world "
new_string = string.strip()
print(new_string) # 输出结果为"hello world"
```
strip()方法可以去除字符串首尾的空格,并返回一个新的字符串。如果想去除字符串中间的空格,可以使用replace()方法将空格替换为空字符串,示例如下:
```Python
string = " hello world "
new_string = string.replace(" ", "")
print(new_string) # 输出结果为"helloworld"
```
另外,如果只想去除字符串开头的空格,可以使用lstrip()方法;如果只想去除字符串末尾的空格,可以使用rstrip()方法。
--结束END--
本文标题: Python怎么去除字符串首尾空格
本文链接: https://www.lsjlt.com/news/417320.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0