python字符串去除空格,python去除字符串(string)空格的五种方法

成年人的爱情不仅仅是简单的我爱你和漂亮的新衣服。

python去掉字符串中的空格的方法总结

1、strip方法去掉字符串两边(开头和结尾)的空格

space_str = ' Remove the space around the string '

print(space_str.strip())

2、lstrip方法去掉字符串左边的空格

left_space_str = ' Remove the space to the left of the string '

print(left_space_str.lstrip())

3、rstrip方法去掉字符串右边的空格

right_space_str = ' Remove the space to the right of the string '

print(right_space_str.rstrip())

4、replace方法替换字符串的空格为空

all_space_str = ' The space of the replacement string is empty '

print(all_space_str.replace(' ', '')

注意:这里说一下replace方法的具体用法

str.replace(old_str, new_str, [max])

old_str:代表原来的字符串,new_str:代表替换之后的字符串,max:代表替换的次数

5、正则匹配替换空格

import re

all_str = " Regular matching without spaces "

print(re.sub(r"\s+", "", all_str))

正则方法的使用这里不多说了,自己查一下详细文档即可。

如果感觉本文对您有帮助可以点个赞哦

本文为学习笔记,转载请标明出处

本文仅供交流学习,请勿用于非法途径

仅是个人意见,如有想法,欢迎留言

  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值