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实现)
Dec 09 Python
速记Python布尔值
Nov 09 Python
python实现flappy bird游戏
Dec 24 Python
Django中使用Whoosh进行全文检索的方法
Mar 31 Python
python实现键盘输入的实操方法
Jul 16 Python
基于MATLAB和Python实现MFCC特征参数提取
Aug 13 Python
python requests证书问题解决
Sep 05 Python
Django框架之中间件MiddleWare的实现
Dec 30 Python
使用 pytorch 创建神经网络拟合sin函数的实现
Feb 24 Python
python中os包的用法
Jun 01 Python
解决Keras中CNN输入维度报错问题
Jun 29 Python
Python中tqdm的使用和例子
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 Session变量不能传送到下一页的解决方法
2009/11/27 PHP
PHP Reflection API详解
2015/05/12 PHP
CI操作cookie的方法分析(基于helper类库)
2016/03/28 PHP
js 页面输出值
2008/11/30 Javascript
javascript 进度条 实现代码
2009/07/30 Javascript
广泛收集的jQuery拖放插件集合
2012/04/09 Javascript
设置jQueryUI DatePicker默认语言为中文
2016/06/04 Javascript
浅析$.getJSON异步请求和同步请求
2016/06/06 Javascript
Augularjs-起步详解
2016/07/08 Javascript
js实现楼层效果的简单实例
2016/07/15 Javascript
bootstrap表格分页实例讲解
2016/12/30 Javascript
vue仿element实现分页器效果
2018/09/13 Javascript
vue随机验证码组件的封装实现
2020/02/19 Javascript
9个JavaScript日常开发小技巧
2020/10/06 Javascript
小程序中手机号识别的示例
2020/12/14 Javascript
Vue 组件注册全解析
2020/12/17 Vue.js
微信小程序学习之自定义滚动弹窗
2020/12/20 Javascript
[01:08:44]NB vs VP 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
Python中asyncore的用法实例
2014/09/29 Python
Python的Django框架可适配的各种数据库介绍
2015/07/15 Python
python 循环遍历字典元素的简单方法
2016/09/11 Python
详解python string类型 bytes类型 bytearray类型
2017/12/16 Python
Python基于csv模块实现读取与写入csv数据的方法
2018/01/18 Python
Python面向对象之继承代码详解
2018/01/29 Python
浅谈pandas中shift和diff函数关系
2018/04/08 Python
python中使用 xlwt 操作excel的常见方法与问题
2019/01/13 Python
Keras模型转成tensorflow的.pb操作
2020/07/06 Python
Pycharm 解决自动格式化冲突的设置操作
2021/01/15 Python
美国折扣宠物药房:Total Pet Supply
2018/05/27 全球购物
可爱的童装和鞋子:Fabkids
2019/08/16 全球购物
护士岗前培训自我评鉴
2014/02/28 职场文书
讲文明树新风演讲稿
2014/05/12 职场文书
外贸会计专业自荐信
2014/06/22 职场文书
2015年学生会干事工作总结
2015/04/09 职场文书
运动会闭幕式主持词
2015/07/01 职场文书
MySQL系列之十五 MySQL常用配置和性能压力测试
2021/07/02 MySQL