Python去除字符串两端空格的方法
目的
获得一个首尾不含多余空格的字符串
方法
可以使用字符串的以下方法处理:
string.lstrip(s[, chars])
Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.
string.rstrip(s[, chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.
string.strip(s[, chars])
Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.
具体的效果如下:
In [10]: x=' Hi,Jack! '
In [11]: print '|',x.lstrip(),'|',x.rstrip(),'|',x.strip(),'|'
| Hi,Jack! | Hi,Jack! | Hi,Jack! |
其中提供的参数chars用来删除特定的符号,注意空格并没有被移除,例如:
In [12]: x='yxyxyxxxyy Hello xyxyxyy'
In [13]: print x.strip('xy')
Hello
相关文章
详解Pandas中stack()和unstack()的使用技巧
当你在处理包含某种序列(例如时间序列数据)的变量的数据集时,数据通常需要进行重塑。Pandas 提供了各种用于重塑 DataFrame 的内置方法。其中,stack() 和 unstack() 是最流行的,本文总结了这两个方法的7种使用技巧,需要的可以参考一下2022-03-03
Pytorch DataLoader shuffle验证方式
这篇文章主要介绍了Pytorch DataLoader shuffle验证方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06
python中numpy.zeros(np.zeros)的使用方法
下面小编就为大家带来一篇python中numpy.zeros(np.zeros)的使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-11-11
python 爬取豆瓣电影短评并利用wordcloud生成词云图
这篇文章主要介绍了python 爬取豆瓣电影短评并利用wordcloud生成词云图,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-06-06


最新评论