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 相关文章推荐
Using Django with GAE Python 后台抓取多个网站的页面全文
Feb 17 Python
Python SMTP发送邮件遇到的一些问题及解决办法
Oct 24 Python
详解用Python练习画个美队盾牌
Mar 23 Python
Python3.5装饰器原理及应用实例详解
Apr 30 Python
Python实现将字符串的首字母变为大写,其余都变为小写的方法
Jun 11 Python
python ChainMap 合并字典的实现步骤
Jun 11 Python
Python跳出多重循环的方法示例
Jul 03 Python
Python3+PyInstall+Sciter解决报错缺少dll、html等文件问题
Jul 15 Python
解决python明明pip安装成功却找不到包的问题
Aug 28 Python
执行Python程序时模块报错问题
Mar 26 Python
浅谈如何使用python抓取网页中的动态数据实现
Aug 17 Python
python matplotlib库的基本使用
Sep 23 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封装的表单验证类完整实例
2016/10/19 PHP
PHP验证码类ValidateCode解析
2017/01/07 PHP
javascript判断单选框或复选框是否选中方法集锦
2007/04/04 Javascript
escape、encodeURI 和 encodeURIComponent 的区别
2009/03/02 Javascript
jQuery 动画弹出窗体支持多种展现方式
2010/04/29 Javascript
javascript 文章截取部分无损html显示实现代码
2010/05/04 Javascript
简单常用的幻灯片播放实现代码
2013/09/25 Javascript
jQuery树形下拉菜单特效代码分享
2015/08/15 Javascript
jQuery实现带延迟效果的滑动菜单代码
2015/09/02 Javascript
不想让浏览器运行javascript脚本的方法
2015/11/20 Javascript
ssm+vue前后端分离框架整合实现(附源码)
2020/07/08 Javascript
python获取网页状态码示例
2014/03/30 Python
python根据出生年份简单计算生肖的方法
2015/03/27 Python
python将文本转换成图片输出的方法
2015/04/28 Python
Python基于递归算法求最小公倍数和最大公约数示例
2018/07/27 Python
python 动态生成变量名以及动态获取变量的变量名方法
2019/01/20 Python
完美解决python3.7 pip升级 拒绝访问问题
2019/07/12 Python
python实现一行输入多个值和一行输出多个值的例子
2019/07/16 Python
基于python实现计算且附带进度条代码实例
2020/03/31 Python
Python判断三段线能否构成三角形的代码
2020/04/12 Python
python实现学生管理系统开发
2020/07/24 Python
python中的yield from语法快速学习
2020/11/06 Python
CSS3等相关属性制作分页导航实现代码
2012/12/24 HTML / CSS
匈牙利墨盒和碳粉购买网站:CDRmarket
2018/04/14 全球购物
编写用C语言实现的求n阶阶乘问题的递归算法
2014/10/21 面试题
linux面试题参考答案(10)
2013/11/04 面试题
办公室文书岗位职责
2013/12/16 职场文书
英语专业学生的自我评价
2013/12/30 职场文书
优秀毕业生求职信范文
2014/01/02 职场文书
计算机专业职业生涯规划范文
2014/01/19 职场文书
应用英语专业自荐信
2014/01/26 职场文书
部队党性分析材料
2014/02/16 职场文书
高中毕业生的个人自我评价
2014/02/21 职场文书
校园环保标语
2014/06/13 职场文书
班级联欢会主持词
2015/07/03 职场文书
pd.drop_duplicates删除重复行的方法实现
2022/06/16 Python