python去除空格的方法有哪些

   2024-10-25 6360
核心提示:使用strip()方法去除字符串两端的空格:s = hello worldprint(s.strip())# Output: hello world使用replace()方法替换空格:s =

使用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 res = "  hello world  "print(re.sub(r"\s+", "", s))# Output: "helloworld"

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号