python去除空格的方法有哪些
- 使用strip()方法去除字符串两端的空格:
s = " hello world "
print(s.strip())
# Output: "hello world"
- 使用replace()方法替换空格:
s = " hello world "
print(s.replace(" ", ""))
# Output: "helloworld"
- 使用join()方法和split()方法去除所有空格:
s = " hello world "
print("".join(s.split()))
# Output: "helloworld"
- 使用正则表达式re.sub()去除所有空格:
import re
s = " hello world "
print(re.sub(r"\s+", "", s))
# Output: "helloworld"
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:mysql获取当前时间的方法有哪些 下一篇:购买幻兽帕鲁服务器怎么选择操作系统
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。