首页 > 编程语言 > 详细

随笔_Python3 去除字符串中的空格

时间:2019-12-07 10:48:21      阅读:123      评论:0      收藏:0      [点我收藏+]

1. strip()方法:

1 str1 =  a b c d e 
2 print(str1.strip())
3 
4 # ‘a b c d e‘

>> 移除字符串首尾指定的字符(默认为空格或换行符)或字符序列。

>> .lstrip() 和 .rstrip() 方法分别指定截取左边或右边的字符

1 str2 = "     this is string example....wow!!!     "
2 print(str2.lstrip())
3 str3 = "88888888this is string example....wow!!!8888888"
4 print(str3.rstrip(8))
5 
6 # this is string example....wow!!!     
7 # 88888888this is string example....wow!!!

 

2. replace()方法:

该方法主要用于字符串的替换replace(old, new, max)

>>返回字符串中的 old(旧字符串) 替换成 new (新字符串)后生成的新字符串,如果指定第三个参数 max,则替换不超过 max 次。

1 str1 = " a b c d e "
2 print(str1 .replace(" ", ""))
3 
4 # ‘abcde‘

 

3. join()+split()方法:

1 str1 =  a b c d e 
2 "".join(str1.split( ))
3 
4 # abcde

 

随笔_Python3 去除字符串中的空格

原文:https://www.cnblogs.com/Raine/p/12000819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!