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实现的可以拷贝或剪切一个文件列表中的所有文件
Apr 30 Python
python实现绘制树枝简单示例
Jul 24 Python
python 自动化将markdown文件转成html文件的方法
Sep 23 Python
Django验证码的生成与使用示例
May 20 Python
python基础while循环及if判断的实例讲解
Aug 25 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 Python
Win10下Python3.7.3安装教程图解
Jul 08 Python
详解Django-channels 实现WebSocket实例
Aug 22 Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 Python
python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例
Feb 25 Python
python实现提取str字符串/json中多级目录下的某个值
Feb 27 Python
python 列表推导和生成器表达式的使用
Feb 01 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 向访客和爬虫显示不同的内容
2009/11/09 PHP
PHP基于IMAP收取邮件的方法示例
2017/08/07 PHP
JS将光标聚焦在文本最后的实现代码
2014/03/28 Javascript
jQuery+PHP+MySQL实现无限级联下拉框效果
2016/02/19 Javascript
JavaScript中日常收集常见的10种错误(推荐)
2017/01/08 Javascript
Bootstrap3下拉菜单的实现
2017/02/22 Javascript
Angular.js ng-file-upload结合springMVC的使用教程
2017/07/10 Javascript
VUE Error: getaddrinfo ENOTFOUND localhost
2018/05/03 Javascript
详解webpack4.x之搭建前端开发环境
2019/03/28 Javascript
python爬虫入门教程之点点美女图片爬虫代码分享
2014/09/02 Python
在windows系统中实现python3安装lxml
2016/03/23 Python
Python数据类型详解(四)字典:dict
2016/05/12 Python
Python对文件操作知识汇总
2016/05/15 Python
Python文件夹与文件的相关操作(推荐)
2016/07/25 Python
利用Python命令行传递实例化对象的方法
2016/11/02 Python
Python实现OpenCV的安装与使用示例
2018/03/30 Python
python xpath获取页面注释的方法
2019/01/14 Python
python实现公司年会抽奖程序
2019/01/22 Python
Django uwsgi Nginx 的生产环境部署详解
2019/02/02 Python
Python内存泄漏和内存溢出的解决方案
2020/09/26 Python
HTML5实现页面切换激活的PageVisibility API使用初探
2016/05/13 HTML / CSS
体验完美剃须:The Art of Shaving
2018/08/06 全球购物
同程旅游英文网站:LY.com
2018/11/13 全球购物
精选奢华:THE LIST
2019/09/05 全球购物
Order by的几种用法
2013/06/16 面试题
我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
2014/03/30 面试题
机电一体化专业应届本科生求职信
2013/09/27 职场文书
高中毕业的自我鉴定
2013/12/09 职场文书
上课说话检讨书大全
2014/01/22 职场文书
中学生运动会通讯稿大全
2014/09/18 职场文书
2014年基层党建工作总结
2014/11/11 职场文书
给老师的感谢信
2015/01/20 职场文书
2015年工会工作总结范文
2015/07/23 职场文书
公司车队管理制度
2015/08/04 职场文书
2019年警察入党转正申请书最新范文
2019/09/03 职场文书
如何有效防止sql注入的方法
2021/05/25 SQL Server