string去空格的有甚么方法
有以下几种方法可以去除字符串中的空格:
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"
这些方法都可以去除字符串中的空格,选择哪一种方法取决于具体的需求和使用处景。
TOP