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 相关文章推荐
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
Dec 04 Python
从零学Python之入门(五)缩进和选择
May 27 Python
python实现自动登录人人网并访问最近来访者实例
Sep 26 Python
python如何将图片转换为字符图片
Aug 19 Python
Python(Django)项目与Apache的管理交互的方法
May 16 Python
python实现windows下文件备份脚本
May 27 Python
Pandas GroupBy对象 索引与迭代方法
Nov 16 Python
Python3 jupyter notebook 服务器搭建过程
Nov 30 Python
python 调用pyautogui 实时获取鼠标的位置、移动鼠标的方法
Aug 27 Python
关于Tensorflow使用CPU报错的解决方式
Feb 05 Python
python实现暗通道去雾算法的示例
Sep 27 Python
用Python提取PDF表格的方法
Apr 11 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中一个控制字符串输出的函数
2006/10/09 PHP
关于PHP5 Session生命周期介绍
2010/03/02 PHP
原生JS实现Ajax通过GET方式与PHP进行交互操作示例
2018/05/12 PHP
List the Codec Files on a Computer
2007/06/11 Javascript
javascript下查找父节点的简单方法
2007/08/13 Javascript
jQuery实现的原图对比窗帘效果
2014/06/15 Javascript
JQuery 给元素绑定click事件多次执行的解决方法
2014/09/09 Javascript
jQuery学习笔记之jQuery中的$
2015/01/19 Javascript
javascript原型模式用法实例详解
2015/06/04 Javascript
js类式继承与原型式继承详解
2016/04/07 Javascript
JavaScript 随机验证码的生成实例代码
2016/09/22 Javascript
js document.getElementsByClassName的使用介绍与自定义函数
2016/11/25 Javascript
Node.js实现文件上传的示例
2017/06/28 Javascript
Node.js如何使用Diffie-Hellman密钥交换算法详解
2017/09/05 Javascript
React Native 通告消息竖向轮播组件的封装
2020/08/25 Javascript
Express系列之multer上传的使用
2017/10/27 Javascript
详解Vue 中 extend 、component 、mixins 、extends 的区别
2017/12/20 Javascript
Vue监听一个数组id是否与另一个数组id相同的方法
2018/09/26 Javascript
如何实现一个简易版的vuex持久化工具
2019/09/11 Javascript
浅析微信小程序modal弹窗关闭默认会执行cancel问题
2019/10/14 Javascript
详解Vue串联过滤器的使用场景
2020/04/30 Javascript
[03:12]2016完美“圣”典风云人物:单车专访
2016/12/02 DOTA
python动态性强类型用法实例
2015/05/09 Python
python对矩阵进行转置的2种处理方法
2019/07/17 Python
python实现简单学生信息管理系统
2020/04/09 Python
基于python生成英文版词云图代码实例
2020/05/16 Python
解决Keras中循环使用K.ctc_decode内存不释放的问题
2020/06/29 Python
利用CSS3的3D效果制作正方体
2020/03/10 HTML / CSS
HTML5-WebSocket实现聊天室示例
2016/12/15 HTML / CSS
Space NK英国站:英国热门美妆网站
2017/12/11 全球购物
美国批发供应商:Kole Imports
2019/04/10 全球购物
会计应聘求职信范文
2013/12/17 职场文书
陈胜吴广起义口号
2014/06/20 职场文书
三八妇女节寄语
2015/02/27 职场文书
基于JavaScript实现省市联动效果
2021/06/22 Javascript
vue项目配置sass及引入外部scss文件
2022/04/14 Vue.js