新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

python去除空格的有什么方法,python去除空格trim

发布时间:2024-04-01 21:01:09

python去除空格的有甚么方法

  1. 使用strip()方法去除字符串两真个空格:
s = "  hello world  "
print(s.strip())
# Output: "hello world"
  1. 使用replace()方法替换空格:
s = "  hello world  "
print(s.replace(" ", ""))
# Output: "helloworld"
  1. 使用join()方法和split()方法去除所有空格:
s = "  hello world  "
print("".join(s.split()))
# Output: "helloworld"
  1. 使用正则表达式re.sub()去除所有空格:
import re
s = "  hello world  "
print(re.sub(r"s+", "", s))
# Output: "helloworld"