Python去除字符串两端空格的方法

 更新时间:2015年05月21日 09:53:29   投稿:junjie  
这篇文章主要介绍了Python去除字符串两端空格的方法,本文主要讲解了string.lstrip、string.rstrip、string.strip等函数的运用,需要的朋友可以参考下

目的

  获得一个首尾不含多余空格的字符串

方法

可以使用字符串的以下方法处理:

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

相关文章

  • Python调用scp向服务器上传文件示例

    Python调用scp向服务器上传文件示例

    今天小编就为大家分享一篇Python调用scp向服务器上传文件示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • 详解Pandas中stack()和unstack()的使用技巧

    详解Pandas中stack()和unstack()的使用技巧

    当你在处理包含某种序列(例如时间序列数据)的变量的数据集时,数据通常需要进行重塑。Pandas 提供了各种用于重塑 DataFrame 的内置方法。其中,stack() 和 unstack() 是最流行的,本文总结了这两个方法的7种使用技巧,需要的可以参考一下
    2022-03-03
  • Python如何检测项目哪些依赖库没有使用

    Python如何检测项目哪些依赖库没有使用

    这篇文章主要为大家详细介绍了五个Python检测项目中哪些依赖库没有使用的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-04-04
  • Pytorch DataLoader shuffle验证方式

    Pytorch DataLoader shuffle验证方式

    这篇文章主要介绍了Pytorch DataLoader shuffle验证方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Python 在局部变量域中执行代码

    Python 在局部变量域中执行代码

    这篇文章主要介绍了Python 如何在局部变量域中执行代码,帮助大家更好的理解和学习Python,感兴趣的朋友可以了解下
    2020-08-08
  • python基于exchange函数发送邮件过程详解

    python基于exchange函数发送邮件过程详解

    这篇文章主要介绍了python基于exchange函数发送邮件过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • python中numpy.zeros(np.zeros)的使用方法

    python中numpy.zeros(np.zeros)的使用方法

    下面小编就为大家带来一篇python中numpy.zeros(np.zeros)的使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • python 爬取豆瓣电影短评并利用wordcloud生成词云图

    python 爬取豆瓣电影短评并利用wordcloud生成词云图

    这篇文章主要介绍了python 爬取豆瓣电影短评并利用wordcloud生成词云图,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-06-06
  • python 根据列表批量下载网易云音乐的免费音乐

    python 根据列表批量下载网易云音乐的免费音乐

    这篇文章主要介绍了python 根据列表下载网易云音乐的免费音乐,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-12-12
  • Apache,wsgi,django 程序部署配置方法详解

    Apache,wsgi,django 程序部署配置方法详解

    这篇文章主要介绍了Apache,wsgi,django 程序部署配置方法,结合实例形式详细分析了Linux环境下Apache,wsgi,django程序部署配置的相关操作技巧与注意事项,需要的朋友可以参考下
    2019-07-07

最新评论