扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python怎么去除文本的空格

扬州沐宇科技
2023-11-13 10:21:35
python

在Python中,可以使用字符串的 strip() 方法去除文本的空格。strip() 方法会删除字符串开头和结尾的空格。

示例代码如下:

text = "   hello world   "
new_text = text.strip()
print(new_text)  # 输出结果为 "hello world"

如果要删除字符串中所有的空格,可以使用 replace() 方法将空格替换为空字符串。

示例代码如下:

text = "   hello world   "
new_text = text.replace(" ", "")
print(new_text)  # 输出结果为 "helloworld"

扫码添加客服微信