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. Ionic 发布可重用代码到NPM上

    1.首先下载可重用模板 https://github.com/ionic-team/ionic-module-template 2. 将模板改包名后发布到NPM上 如果你没有NPM账号,先进行注册. ...

  2. NLB网路负载均衡管理

    相对于ARR来说,ARR算是应用级别的负载均衡方案,而NLB则是服务器级别的负载均衡方案.ARR只能做请求入口的消息分发服务,这样如果我们的消息分发服务器给挂掉,那么做再多的应用服务集群也都枉然. A ...

  3. MySQL数据库分区操作【RANGE】

    客服平台,线上查询存在性能问题,为了解决或者说是缓解这个问题,除了加必要的索引,另外就是将表进行分区. 这里主要是针对既有的表进行分区,采用的是alter table xxx的方式,当然,也可以采用c ...

  4. 基于spring的placeholder思路处理配置信息敏感信息加密解密的实践

    基于Spring的placeholder处理思路,实现系统配置信息敏感信息的加密解密处理. 我们的处理方案,是基于类org.springframework.beans.factory.config.P ...

  5. js中return ,return true,return false;区别

    js中return:.return true.return false;区别 转:https://www.cnblogs.com/camikehuihui/p/7999537.html 一.返回控制与 ...

  6. STM32 printf函数

    /******************** (C) COPYRIGHT 2012 WildFire Team *************************** * 文件名 :usart1.c * ...

  7. Azure Application Gateway (5) Application Gateway SSL Offload配置

    <Windows Azure Platform 系列文章目录> 之前有个客户提出了一个需求,他们的互联网访问的架构分为两种: 1.第一层是使用Azure Application Gatew ...

  8. PHP操作mongoDB 笔记

    转自 http://blog.csdn.net/black_ox/article/details/22678747 命令也可以在参考http://www.jb51.net/article/51601. ...

  9. IntelliJ Idea 跳出括号并且光标移到末尾的快捷键

    直接跳出的shift enter不管现在光标在哪个位置,直接新开一行 跳出双引号:shift + "跳出单引号:'跳出括号:shift + )跳出中括号:]以此类推.

  10. PHP 数据运算类型都有哪些?

    四种标量类型:布尔型 boolean $bo=TRUE; $bo=FALSE;整型 integer  $bo=1; $bo=-12;浮点型 float/double  $bo=1.001; $bo=3 ...