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中contextlib上下文管理模块的用法
Jun 28 Python
在java中如何定义一个抽象属性示例详解
Aug 18 Python
浅谈python爬虫使用Selenium模拟浏览器行为
Feb 23 Python
python求质数的3种方法
Sep 28 Python
pycharm运行程序时在Python console窗口中运行的方法
Dec 03 Python
python进行文件对比的方法
Dec 24 Python
Python类的继承、多态及获取对象信息操作详解
Feb 28 Python
python基于itchat模块实现微信防撤回
Apr 29 Python
用scikit-learn和pandas学习线性回归的方法
Jun 21 Python
Python中logging日志库实例详解
Feb 19 Python
PyQt5 如何让界面和逻辑分离的方法
Mar 24 Python
Django如何重置migration的几种情景
Feb 24 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/09/22 PHP
PHP基于Redis消息队列实现发布微博的方法
2017/05/03 PHP
PHP判断是否是微信打开,浏览器打开的方法
2018/03/14 PHP
php实现微信公众平台发红包功能
2018/06/14 PHP
统计PHP目录中的文件数方法
2019/03/05 PHP
关于js new Date() 出现NaN 的分析
2012/10/23 Javascript
javascript利用控件对windows的操作实现原理与应用
2012/12/23 Javascript
jQuery中 noConflict() 方法使用
2013/04/25 Javascript
js检验密码强度(低中高)附图
2014/06/05 Javascript
node.js中的http.get方法使用说明
2014/12/14 Javascript
javascript事件委托的方式绑定详解
2015/06/10 Javascript
详解JavaScript的策略模式编程
2015/06/24 Javascript
JS实现三级折叠菜单特效,其它级可自动收缩
2015/08/06 Javascript
JavaScript判断对象是否为数组
2015/12/22 Javascript
Javascript实现购物车功能的详细代码
2018/05/08 Javascript
JavaScript事件发布/订阅模式原理与用法分析
2018/08/21 Javascript
Javascript之高级数组API的使用实例
2019/03/08 Javascript
vue封装swiper代码实例解析
2019/10/08 Javascript
JS实现放烟花效果
2020/03/10 Javascript
跟老齐学Python之做一个小游戏
2014/09/28 Python
python在windows下实现ping操作并接收返回信息的方法
2015/03/20 Python
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
2015/04/11 Python
在Linux中通过Python脚本访问mdb数据库的方法
2015/05/06 Python
Python 基础教程之闭包的使用方法
2017/09/29 Python
对Python 窗体(tkinter)树状数据(Treeview)详解
2018/10/11 Python
详解html5页面 rem 布局适配方法
2018/01/12 HTML / CSS
美国网上眼镜商城:Zenni Optical
2016/11/20 全球购物
英国时尚服饰电商:Boohoo
2017/10/12 全球购物
你在项目中用到了xml技术的哪些方面?如何实现的?
2014/01/26 面试题
重写子类方法时,抛出异常的书写注意事项
2015/10/17 面试题
岗位竞聘演讲稿
2014/01/10 职场文书
优秀学生干部推荐材料
2014/02/03 职场文书
领导干部四风问题自我剖析材料
2014/09/25 职场文书
2014年勤工助学工作总结
2014/11/24 职场文书
总经理检讨书范文
2015/02/16 职场文书
工作年限证明模板
2015/06/15 职场文书