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列表(list)、字典(dict)、字符串(string)基本操作小结
Nov 28 Python
Python Web框架Flask中使用百度云存储BCS实例
Feb 08 Python
Python实现文件按照日期命名的方法
Jul 09 Python
Python的Flask框架应用程序实现使用QQ账号登录的方法
Jun 07 Python
python的paramiko模块实现远程控制和传输示例
Oct 13 Python
详谈python在windows中的文件路径问题
Apr 28 Python
关于python多重赋值的小问题
Apr 17 Python
pandas计数 value_counts()的使用
Jun 24 Python
python网络编程之五子棋游戏
May 14 Python
Spring http服务远程调用实现过程解析
Jun 11 Python
Python面向对象多态实现原理及代码实例
Sep 16 Python
python基于机器学习预测股票交易信号
May 25 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 存储文本换行实现方法
2010/01/05 PHP
php iconv() : Detected an illegal character in input string
2010/12/05 PHP
PHP上传文件时文件过大$_FILES为空的解决方法
2013/11/26 PHP
PHP中使用memcache存储session的三种配置方法
2014/04/05 PHP
ThinkPHP框架设计及扩展详解
2014/11/25 PHP
总结PHP中DateTime的常用方法
2016/08/11 PHP
Avengerls vs KG BO3 第二场2.18
2021/03/10 DOTA
javascript 打印内容方法小结
2009/11/04 Javascript
javascript中的prototype属性使用说明(函数功能扩展)
2010/08/16 Javascript
myEvent.js javascript跨浏览器事件框架
2011/10/24 Javascript
FF火狐下获取一个元素同类型的相邻元素实现代码
2012/12/15 Javascript
jquery原创弹出层折叠效果点击折叠弹出一个层
2014/03/12 Javascript
JQuery EasyUI 加载两次url的原因分析及解决方案
2014/08/18 Javascript
jQuery中:eq()选择器用法实例
2014/12/29 Javascript
jQuery定义插件的方法
2015/12/18 Javascript
javascript闭包概念简单解析(推荐)
2016/06/03 Javascript
浅谈bootstrap源码分析之scrollspy(滚动侦听)
2016/06/06 Javascript
BootStrapTable服务器分页实例解析
2016/12/20 Javascript
jQuery实现动态添加tr到table的方法
2016/12/26 Javascript
Vue波纹按钮组件制作
2018/04/30 Javascript
如何实现小程序tab栏下划线动画效果
2019/05/18 Javascript
在 Vue 中编写 SVG 图标组件的方法
2020/02/24 Javascript
详解vue修改elementUI的分页组件视图没更新问题
2020/11/13 Javascript
[42:22]DOTA2上海特级锦标赛C组小组赛#1 OG VS Archon第一局
2016/02/27 DOTA
django 做 migrate 时 表已存在的处理方法
2019/08/31 Python
Python Tkinter Entry和Text的添加与使用详解
2020/03/04 Python
pytorch  网络参数 weight bias 初始化详解
2020/06/24 Python
苹果美国官方商城:Apple美国
2016/08/24 全球购物
Kingsoft金山公司C/C++笔试题
2016/05/10 面试题
简述安装Slackware Linux系统的过程
2012/05/08 面试题
办公室经理岗位职责
2014/01/01 职场文书
幼儿园保教管理制度
2014/02/03 职场文书
市贸粮局召开党的群众路线教育实践活动总结大会新闻稿
2014/10/21 职场文书
高中生毕业评语
2014/12/30 职场文书
Redis延迟队列和分布式延迟队列的简答实现
2021/05/13 Redis
MySQL实战记录之如何快速定位慢SQL
2022/03/23 MySQL