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实现DNS正向查询、反向查询的例子
Apr 25 Python
Python实现合并同一个文件夹下所有PDF文件的方法示例
Apr 28 Python
根据DataFrame某一列的值来选择具体的某一行方法
Jul 03 Python
浅谈Python里面小数点精度的控制
Jul 16 Python
Python 获取div标签中的文字实例
Dec 20 Python
Python利用神经网络解决非线性回归问题实例详解
Jul 19 Python
从numpy数组中取出满足条件的元素示例
Nov 26 Python
关于numpy数组轴的使用详解
Dec 05 Python
Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的
Apr 20 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
May 16 Python
python编程项目中线上问题排查与解决
Nov 01 Python
Python 文字识别
May 11 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
德劲1102收音机的打理维修案例
2021/03/02 无线电
一个简单的域名注册情况查询程序
2006/10/09 PHP
PHP实现生成透明背景的PNG缩略图函数分享
2014/07/08 PHP
php读取本地json文件的实例
2018/03/07 PHP
Yii框架函数简单用法分析
2019/09/09 PHP
php 函数中静态变量使用的问题实例分析
2020/03/05 PHP
JSON扫盲帖 JSON.as类教程
2009/02/16 Javascript
基于jquery实现漂亮的动态信息提示效果
2011/08/02 Javascript
JQuery查找DOM节点的方法
2015/06/11 Javascript
JavaScript中解决多浏览器兼容性23个问题的快速解决方法
2016/05/19 Javascript
JavaScript基于Dom操作实现查找、修改HTML元素的内容及属性的方法
2017/01/20 Javascript
Angular2数据绑定详解
2017/04/18 Javascript
关于jQuery中fade(),show()起始位置的一点小发现
2017/04/25 jQuery
Angular2中监听数据更新的方法
2018/08/31 Javascript
javascript将非数值转换为数值
2018/09/13 Javascript
原生JS forEach()和map()遍历的区别、兼容写法及jQuery $.each、$.map遍历操作
2019/02/27 jQuery
微信小程序实现打开并下载服务器上面的pdf文件到手机
2019/09/20 Javascript
JS实现audio音频剪裁剪切复制播放与上传(步骤详解)
2020/07/28 Javascript
微信小程序实现底部弹出模态框
2020/11/18 Javascript
Node.js中的异步生成器与异步迭代详解
2021/01/31 Javascript
详解微信小程序(Taro)手动埋点和自动埋点的实现
2021/03/02 Javascript
[01:06:43]完美世界DOTA2联赛PWL S3 PXG vs GXR 第二场 12.19
2020/12/24 DOTA
Python中subprocess模块用法实例详解
2015/05/20 Python
讲解Python中fileno()方法的使用
2015/05/24 Python
在centos7中分布式部署pyspider
2017/05/03 Python
python实现批量图片格式转换
2020/06/16 Python
selenium+python设置爬虫代理IP的方法
2018/11/29 Python
详解使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件
2019/08/23 Python
纯css实现照片墙3D效果的示例代码
2017/11/13 HTML / CSS
加拿大高尔夫超市:Golf Town
2018/01/12 全球购物
什么是动态端口(Dynamic Ports)?动态端口的范围是多少?
2014/12/12 面试题
UNIX文件系统分类
2014/11/11 面试题
委托书怎么写
2014/07/31 职场文书
瘦西湖导游词
2015/02/03 职场文书
2016年企业安全生产月活动总结
2016/04/06 职场文书
【HBU】数据库第四周 单表查询
2021/04/05 SQL Server