Python中字符串去除空格的方法有多种,常用的方法有以下几种:
s = " hello world "
s = s.strip()
print(s) # 输出:hello world
s = " hello world "
s = s.lstrip()
print(s) # 输出:hello world
s = " hello world "
s = s.rstrip()
print(s) # 输出: hello world
s = " hello world "
s = s.replace(" ", "")
print(s) # 输出:helloworld
import re
s = " hello world "
s = re.sub(r"\s", "", s)
print(s) # 输出:helloworld
以上是常用的几种方法,根据具体需求可以选择适合的方法。