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
以上是常用的几种方法,根据具体需求可以选择适合的方法。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>