有以下几种方法可以去除字符串中的空格:
string = "hello world"
string = string.replace(" ", "")
print(string) # 输出"helloworld"
string = "hello world"
string = "".join(string.split())
print(string) # 输出"helloworld"
import re
string = "hello world"
string = re.sub(r"\s", "", string)
print(string) # 输出"helloworld"
这些方法都可以去除字符串中的空格,选择哪种方法取决于具体的需求和使用场景。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: string数组能否存储不同类型数据