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简单检测文本类型的2种方法【基于文件头及cchardet库】
Sep 18 Python
Python端口扫描简单程序
Nov 10 Python
python网络编程调用recv函数完整接收数据的三种方法
Mar 31 Python
Python爬虫抓取代理IP并检验可用性的实例
May 07 Python
Python面向对象之静态属性、类方法与静态方法分析
Aug 24 Python
python实现事件驱动
Nov 21 Python
Kali Linux安装ipython2 和 ipython3的方法
Jul 11 Python
python基于Selenium的web自动化框架
Jul 14 Python
python实现各种插值法(数值分析)
Jul 30 Python
Python使用sqlite3模块内置数据库
May 07 Python
Python偏函数实现原理及应用
Nov 20 Python
python 统计代码耗时的几种方法分享
Apr 02 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
Smarty+QUICKFORM小小演示
2007/02/25 PHP
使用PHPMYADMIN操作mysql数据库添加新用户和数据库的方法
2010/04/02 PHP
php Ubb代码编辑器函数代码
2012/07/05 PHP
php实现修改新闻时删除图片的方法
2015/05/12 PHP
thinkPHP使用pclzip打包备份mysql数据库的方法
2016/04/30 PHP
php实现文章置顶功能的方法
2016/10/20 PHP
多浏览器兼容的获取元素和鼠标的位置的js代码
2009/12/15 Javascript
js中function()使用方法
2013/12/24 Javascript
JavaScript 实现简单的倒计时弹窗DEMO附图
2014/03/05 Javascript
网页运行时提示对象不支持abigimage属性或方法
2014/08/10 Javascript
JavaScript的null和undefined区别示例介绍
2014/09/15 Javascript
Nodejs全栈框架StrongLoop推荐
2014/11/09 NodeJs
浏览器检测JS代码(兼容目前各大主流浏览器)
2016/02/21 Javascript
JQuery插件Marquee.js实现无缝滚动效果
2016/04/26 Javascript
jQuery 常用代码集锦(必看篇)
2016/05/16 Javascript
JS函数多个参数默认值指定方法分析
2016/11/28 Javascript
Bootstrap风格的zTree右键菜单
2017/02/17 Javascript
JavaScript评论点赞功能的实现方法
2017/03/13 Javascript
JS实现数组去重方法总结(六种方法)
2017/07/14 Javascript
Vue Element UI + OSS实现上传文件功能
2019/07/31 Javascript
vue实现购物车选择功能
2020/01/10 Javascript
Vue切换div显示隐藏,多选,单选代码解析
2020/07/14 Javascript
Python 不同对象比较大小示例探讨
2014/08/21 Python
python生成tensorflow输入输出的图像格式的方法
2018/02/12 Python
详解Python中的type和object
2018/08/15 Python
python自动化之Ansible的安装教程
2019/06/13 Python
文件上传服务器-jupyter 中python解压及压缩方式
2020/04/22 Python
Python接收手机短信的代码整理
2020/08/02 Python
详解使用python爬取抖音app视频(appium可以操控手机)
2021/01/26 Python
以色列的身体护理及家居香薰品牌:Sabon NYC
2018/02/23 全球购物
幼儿园秋游感想
2014/03/12 职场文书
2014年祖国生日寄语
2014/09/19 职场文书
庆六一宣传标语
2014/10/08 职场文书
2014年社区民政工作总结
2014/12/02 职场文书
工作自我评价范文
2015/03/05 职场文书
jquery插件实现代码雨特效
2021/04/24 jQuery