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


Posted in Python onMay 21, 2015

目的

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

方法

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

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 相关文章推荐
Python中的Matplotlib模块入门教程
Apr 15 Python
scrapy自定义pipeline类实现将采集数据保存到mongodb的方法
Apr 16 Python
使用Python下载歌词并嵌入歌曲文件中的实现代码
Nov 13 Python
一个基于flask的web应用诞生 bootstrap框架美化(3)
Apr 11 Python
浅谈python日志的配置文件路径问题
Apr 28 Python
python实现弹跳小球
May 13 Python
Tensorflow模型实现预测或识别单张图片
Jul 19 Python
python实现的发邮件功能示例
Sep 11 Python
Python 获取numpy.array索引值的实例
Dec 06 Python
keras的三种模型实现与区别说明
Jul 03 Python
Kmeans均值聚类算法原理以及Python如何实现
Sep 26 Python
Python基于Webhook实现github自动化部署
Nov 28 Python
在Python中处理列表之reverse()方法的使用教程
May 21 #Python
Python中字符串对齐方法介绍
May 21 #Python
在Python的列表中利用remove()方法删除元素的教程
May 21 #Python
Python检测一个对象是否为字符串类的方法
May 21 #Python
在Python中操作列表之List.pop()方法的使用
May 21 #Python
Python字符和字符值(ASCII或Unicode码值)转换方法
May 21 #Python
Python中每次处理一个字符的5种方法
May 21 #Python
You might like
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
2012/01/16 PHP
腾讯QQ微博API接口获取微博内容
2013/10/30 PHP
destoon安装出现Internal Server Error的解决方法
2014/06/21 PHP
PHP实现设计模式中的抽象工厂模式详解
2014/10/11 PHP
JQuery优缺点分析说明
2010/06/09 Javascript
js获取IFRAME当前的URL的方法
2013/11/13 Javascript
IE和Firefox之间在JavaScript语法上的差异
2016/04/22 Javascript
使用jQuery Mobile框架开发移动端Web App的入门教程
2016/05/17 Javascript
JS验证 只能输入小数点,数字,负数的实现方法
2016/10/07 Javascript
JS简单生成随机数(随机密码)的方法
2017/05/11 Javascript
详解VUE 数组更新
2017/12/16 Javascript
微信小程序实现签到功能
2018/10/31 Javascript
JavaScript展开操作符(Spread operator)详解
2019/07/20 Javascript
vue实现移动端省市区选择
2019/09/27 Javascript
ant-design表单处理和常用方法及自定义验证操作
2020/10/27 Javascript
[02:49]2014DOTA2电竞也是体育项目! 势要把荣誉带回中国!
2014/07/20 DOTA
python 生成器协程运算实例
2017/09/04 Python
python async with和async for的使用
2019/06/20 Python
Anaconda之conda常用命令介绍(安装、更新、删除)
2019/10/06 Python
Python通过kerberos安全认证操作kafka方式
2020/06/06 Python
Python局部变量与全局变量区别原理解析
2020/07/14 Python
H5调用相机拍照并压缩图片的实例代码
2017/07/20 HTML / CSS
html5 canvas 实现光线沿不规则路径运动
2020/04/20 HTML / CSS
Perricone MD裴礼康美国官网:抗衰老护肤品
2016/09/26 全球购物
如何转换一个字符串到enum值
2014/04/12 面试题
介绍一下EJB的体系结构
2012/08/01 面试题
大学生职业生涯规划范文
2014/01/08 职场文书
初中学生评语大全
2014/04/24 职场文书
2014年会策划方案
2014/05/11 职场文书
大学生应聘求职信
2014/05/26 职场文书
竞选班干部演讲稿300字
2014/08/20 职场文书
财务负责人岗位职责
2015/02/03 职场文书
关于倡议书的范文
2015/04/29 职场文书
数学备课组工作总结
2015/08/12 职场文书
教你怎么用Python监控愉客行车程
2021/04/29 Python
django中websocket的具体使用
2022/01/22 Python