python字符串去空格的有哪些方法
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
以上是经常使用的几种方法,根据具体需求可以选择合适的方法。
TOP