1.去掉左边空格
string = " * it is blank space test * "
print (string.lstrip())
result:
* it is blank space test *
2.去掉右边空格
string = " * it is blank space test * "
print (string.rstrip())
result:
* it is blank space test *
3.去掉左右两边空格
string = " * it is blank space test * "
print (string.strip())
result:
* it is blank space test *
4.去掉所有空格
有两种方式
string = " * it is blank space test * "
str_new = string.replace(" ", "")
print str_new
result:
*itisblankspacetest*
eg2:正则匹配把空格替换成空
import re
string = " * it is blank space test * "
str_new = re.sub(r"\s+", "", string)
print str_new
result:
*itisblankspacetest*
关于python去掉空格常用方式有哪些,环球青藤小编就和大家分享到这里了,学习是永无止境的,学习一项技能更是受益终身,所以,只要肯努力学,什么时候开始都不晚。如果您还想继续了解关于python编程的学习方法及素材等内容,可以点击本站其他文章学习。
成年人的爱情不仅仅是简单的我爱你和漂亮的新衣服。
上一篇: python3将两个列表合并成字典
下一篇: python3 map()函数
1、strip方法去掉字符串两边(开头和结尾)的空格
2、lstrip方法去掉字符串左边的空格
3、rstrip方法去掉字符串右边的空格
4、replace方法替换字符串的空格为空
注意: 这里说一下replace方法的具体用法
old_str:原字符串需要替换的内容,new_str:将old_str替换成的内容,max:代表替换的次数,默认全部替换
5、正则匹配替换空格
正则方法的使用这里不多说了,自己查一下详细文档即可。
如果感觉本文对您有帮助可以点个赞哦
本文仅供交流学习,请勿用于非法途径
仅是个人意见,如有想法,欢迎留言