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 从远程服务器下载日志文件的程序
Feb 10 Python
Python实现向服务器请求压缩数据及解压缩数据的方法示例
Jun 09 Python
Python实现学生成绩管理系统
Apr 05 Python
python实现求两个字符串的最长公共子串方法
Jul 20 Python
Python简单获取二维数组行列数的方法示例
Dec 21 Python
pyqt5 使用label控件实时显示时间的实例
Jun 14 Python
Python对列表的操作知识点详解
Aug 20 Python
Python利用PyPDF2库获取PDF文件总页码实例
Apr 03 Python
Python常用库Numpy进行矩阵运算详解
Jul 21 Python
浅析Python 多行匹配模式
Jul 24 Python
Scrapy 配置动态代理IP的实现
Sep 28 Python
Python3.8官网文档之类的基础语法阅读
Sep 04 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/07/07 PHP
js 替换功能函数,用正则表达式解决,js的全部替换
2010/12/08 Javascript
如何动态的导入js文件具体该怎么实现
2014/01/14 Javascript
类似天猫商品详情随浏览器移动的示例代码
2014/02/27 Javascript
jQuery插件windowScroll实现单屏滚动特效
2015/07/14 Javascript
javascript中判断json的方法总结
2015/08/27 Javascript
jQuery 实现评论等级好评差评特效
2016/05/06 Javascript
bootstrap中使用google prettify让代码高亮的方法
2016/10/21 Javascript
基于webpack 实用配置方法总结
2017/09/28 Javascript
使用VUE+iView+.Net Core上传图片的方法示例
2019/01/04 Javascript
vue自动化路由的实现代码
2019/09/30 Javascript
谈谈JavaScript中的函数
2020/09/08 Javascript
javascript实现简易计算器功能
2020/09/23 Javascript
[04:09]显微镜下的DOTA2第十二期—NaVi美如画的团战
2014/06/23 DOTA
[04:16]DOTA2全国高校联赛16强抽签
2018/05/02 DOTA
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
常见的在Python中实现单例模式的三种方法
2015/04/08 Python
详解Python中的array数组模块相关使用
2016/07/05 Python
django rest framework之请求与响应(详解)
2017/11/06 Python
Python设计模式之中介模式简单示例
2018/01/09 Python
python+matplotlib绘制饼图散点图实例代码
2018/01/20 Python
jupyter notebook引用from pyecharts.charts import Bar运行报错
2020/04/23 Python
python绘制立方体的方法
2018/07/02 Python
Canvas实现贝赛尔曲线轨迹动画的示例代码
2019/04/25 HTML / CSS
iPhoneX安全区域(Safe Area)底部小黑条在微信小程序和H5的屏幕适配
2020/04/08 HTML / CSS
韩国爱茉莉太平洋化妆品美国站:Amore Pacific US
2016/10/28 全球购物
马来西亚在线购物市场:PGMall.my
2019/10/13 全球购物
公司市场专员岗位职责
2014/06/29 职场文书
超市创业计划书
2014/09/15 职场文书
2014年团支部年度工作总结
2014/12/24 职场文书
品牌形象定位,全面分析
2019/07/23 职场文书
基于Golang 高并发问题的解决方案
2021/05/08 Golang
Java用自带的Image IO给图片添加水印
2021/06/15 Java/Android
postgres之jsonb属性的使用操作
2021/06/23 PostgreSQL
Jackson 反序列化时实现大小写不敏感设置
2021/06/29 Java/Android
Python使用Web框架Flask开发项目
2022/06/01 Python