在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"
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python去除空格的方法有哪些