Python strip lstrip rstrip使用方法(字符串处理空格)

 
strip是trim掉字符串两边的空格。
lstrip, trim掉左边的空格
rstrip, trim掉右边的空格
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.

strip lstrip rstrip使用方法

Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边 的字符,rstrip用于去除右边的字符。这三个函数都可传入一个参数,指定要去除的首尾字符。注意的是,传入的是一个字符数组,编译器去除两端所有相应 的字符,直到没有匹配的字符,比如:

theString = 'saaaay yes no yaaaass' 
print theString.strip('say') 
theString依次被去除首尾在['s','a','y']数组内的字符,直到字符在不数组内。所以,输出的结果为: 
yes no 
比较简单吧,lstrip和rstrip原理是一样的。注意:当没有传入参数时,是默认去除首尾空格的。

theString = 'saaaay yes no yaaaass' 
print theString.strip('say') 
print theString.strip('say ') #say后面有空格 
print theString.lstrip('say') 
print theString.rstrip('say') 
运行结果: 
yes no 
es no 
yes no yaaaass 
saaaay yes no

 
 
二:去掉中间空格
 
   按照文档要求,应该可以去除string中的空格,但是结果如何呢?
  1. x = '    hello python   '
  2. print  '|' , x.lstrip( ), '|' , x.rstrip( ), '|' , x.strip( ), '|'

| hello python    |     hello python | hello python |

 
没有成功!!!!!
   不知道那位仁兄能给出原因?谢谢
   有个笨方法:以空格split字符串,然后重新连接
   >>> x_list = x.split(' ')
   >>> y = ''.join(x_list)
   >>> print '|' ,y,'|'
    | hellopython |
 
Success!!

Python strip lstrip rstrip使用方法(字符串处理空格)的更多相关文章

  1. python strip() lstrip() rstrip() 使用方法

    Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除最左边的字符,rstrip用于去除最右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入 ...

  2. ython strip lstrip rstrip使用方法

    Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是一 ...

  3. Python误区之strip,lstrip,rstrip

    最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用targetStr.lstrip(")"),发现在 将处理完的数据插入到数据库时会出现编码报错,于是在网上搜到了这 ...

  4. 【转】Python中string的strip,lstrip,rstrip用法

    Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...

  5. python 知识 rstrip,strip,lstrip

    rstrip,strip,lstrip 作用:去除字符串中的空格或指定字符 一.默认用法:去除空格str.strip()  : 去除字符串两边的空格str.lstrip() : 去除字符串左边的空格s ...

  6. Python关于去除字符串中空格的方法

    Python关于去除字符串中空格的方法 在编写程序时我们经常会遇到需要将字符串中的空格去掉的情况,通常我们可以使用下面几种解决方法: 1.strip()方法:该方法只能把字符串头和尾的空格去掉,但是不 ...

  7. python基础——4(数字、字符串、列表类型的内置方法介绍)

    目录 一.可变与不可变类型 二.数字类型 三.字符串类型 四.列表类型 一.可变与不可变类型 可变类型:值改变,但是id不变,证明就是在改变原值,是可变类型 不可变类型:值改变,id也跟着改变,证明产 ...

  8. python strip()方法使用

    描述 python strip() ,用于去除述字符串头尾指定字符(默认为空格或换行符)或字符序列. 注意:此方法只能去除头尾的空格或是换行符,不能去除中间的. 语法: str.strip([char ...

  9. Python中的数据类型常见方法

    list() 方法 1.cmp(list1, list2):#比较两个列表的元素 2.len(list):#列表元素个数 3.max(list):#返回列表元素最大值 4.min(list):#返回列 ...

随机推荐

  1. c/c++ 获取数组长度

    在C/C++中并没有提供直接获取数组长度的函数 c/c++ 获取数组长度其中一种方法是使用sizeof(array) / sizeof(array[0]). 在C语言中习惯上在使用时都把它定义成一个宏 ...

  2. Springboot配置文件映射

    添加类和配置文件的映射: 1.定义映射类 @Component @PropertySource("classpath:config/XX.properties") public c ...

  3. matchmove流程中修改Maya相机数据的脚本

    matchmove组的流程涉及到syntheyes与maya,nuke交接的问题.syntheyes会生成包含去畸变节点的nuke工程 和带跟踪信息的maya工程.在nuke工程上渲染得到去畸变的序列 ...

  4. 《JavaScript设计模式与开发》笔记 7.单例模式

    废话一箩筐就这个原来 var instance; return function asdf(name){ if(!this.instance){ this.instance = new asdf(na ...

  5. flask-appbuilder 快速入门

    简介: Flask-AppBuilder是基于Flask实现的一个用于快速构建Web后台管理系统的简单的框架.主要用于解决构建Web后台管理系统时避免一些重复而繁琐的工作,提高项目完成时间,它可以和 ...

  6. C# .NET 2.0 判断当前程序进程是否为64位运行时 (x64)

    public static bool Is64BitProcess() { ; }

  7. influxDB1.6版安装与配置(windows环境)、Jmeter+influxDB+Grafana性能监控

    influxDB1.6版安装与配置(windows环境).Jmeter+influxDB+Grafana性能监控 来源:https://blog.csdn.net/SwTesting/article/ ...

  8. <亲测>CentOS 7.3下Node.js 8.6安装配置(含NPM以及PM2)

    CentOS 7.3下Node.js 8.6安装配置 2017年09月30日 14:12:02 阅读数:2245更多 个人分类: Nodejs   版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  9. 【java】之正则表达式摘要

    构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn (0 <= n < ...

  10. gitlab 10.8.1 迁移

    参考官网: https://docs.gitlab.com/ee/raketasks/backup_restore.html    Backing up and restoring GitLab 及 ...