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实现的检测web服务器健康状况的小程序
Sep 17 Python
Python rstrip()方法实例详解
Nov 11 Python
python实现批量注册网站用户的示例
Feb 22 Python
pandas按行按列遍历Dataframe的几种方式
Oct 23 Python
Pycharm创建项目时如何自动添加头部信息
Nov 14 Python
使用 Python 清理收藏夹里已失效的网站
Dec 03 Python
pytorch-RNN进行回归曲线预测方式
Jan 14 Python
tensorflow 获取checkpoint中的变量列表实例
Feb 11 Python
python计算Content-MD5并获取文件的Content-MD5值方式
Apr 03 Python
Python opencv相机标定实现原理及步骤详解
Apr 09 Python
Python列表如何更新值
May 27 Python
python中字典增加和删除使用方法
Sep 30 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检测数组长度函数sizeof与count用法
2014/11/17 PHP
php实现session共享的实例方法
2019/09/19 PHP
JavaScript DOM 学习第五章 表单简介
2010/02/19 Javascript
JavaScript中switch判断容易犯错的一个细节
2014/08/27 Javascript
js判断滚动条是否已到页面最底部或顶部实例
2014/11/20 Javascript
jquery实现从数组移除指定的值
2015/06/24 Javascript
jquery实现平滑的二级下拉菜单效果
2015/08/26 Javascript
jQuery操作Table技巧大汇总
2016/01/23 Javascript
JS实现直接运行html代码的方法
2017/03/13 Javascript
使用jquery的jsonp如何发起跨域请求及其原理详解
2017/08/17 jQuery
在Debian(Raspberry Pi)树莓派上安装NodeJS的教程详解
2017/09/19 NodeJs
微信小程序实现工作时间段选择
2019/02/15 Javascript
vue实现todolist基本功能以及数据存储功能实例详解
2019/04/11 Javascript
微信小程序实现蒙版弹出窗功能
2019/09/17 Javascript
vuecli3.x中轻松4步带你使用tinymce的步骤
2020/06/25 Javascript
[02:40]DOTA2英雄基础教程 炼金术士
2013/12/23 DOTA
[51:53]完美世界DOTA2联赛决赛日 Inki vs LBZS 第二场 11.08
2020/11/10 DOTA
简单学习Python time模块
2016/04/29 Python
Python的Twisted框架中使用Deferred对象来管理回调函数
2016/05/25 Python
python微信跳一跳系列之棋子定位颜色识别
2018/02/26 Python
python docx 中文字体设置的操作方法
2018/05/08 Python
python 简单照相机调用系统摄像头实现方法 pygame
2018/08/03 Python
python 实现创建文件夹和创建日志文件的方法
2019/07/07 Python
python爬虫 2019中国好声音评论爬取过程解析
2019/08/26 Python
windows中安装Python3.8.0的实现方法
2019/11/19 Python
Python接口测试get请求过程详解
2020/02/28 Python
TensorFLow 数学运算的示例代码
2020/04/21 Python
Python实现SMTP邮件发送
2020/06/16 Python
HTML5+CSS3实现拖放(Drag and Drop)示例
2014/07/07 HTML / CSS
英国鞋网:Rubber Sole
2020/03/03 全球购物
会计专业应届生自荐信
2014/02/07 职场文书
倡议书格式模板
2014/05/13 职场文书
2014年商场国庆节活动策划方案
2014/09/16 职场文书
因工资原因离职的辞职信范文
2015/05/12 职场文书
五年级作文之成长
2019/09/16 职场文书
浅谈resultMap的用法及关联结果集映射
2021/06/30 Java/Android