Python strip lstrip rstrip使用方法(字符串处理空格)
Python strip lstrip rstrip使用方法(字符串处理空格)
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使用方法
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
- x = ' hello python '
- print '|' , x.lstrip( ), '|' , x.rstrip( ), '|' , x.strip( ), '|'
| hello python | hello python | hello python |
>>> y = ''.join(x_list)
>>> print '|' ,y,'|'
Python strip lstrip rstrip使用方法(字符串处理空格)的更多相关文章
- python strip() lstrip() rstrip() 使用方法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除最左边的字符,rstrip用于去除最右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入 ...
- ython strip lstrip rstrip使用方法
Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是一 ...
- Python误区之strip,lstrip,rstrip
最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用targetStr.lstrip(")"),发现在 将处理完的数据插入到数据库时会出现编码报错,于是在网上搜到了这 ...
- 【转】Python中string的strip,lstrip,rstrip用法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...
- python 知识 rstrip,strip,lstrip
rstrip,strip,lstrip 作用:去除字符串中的空格或指定字符 一.默认用法:去除空格str.strip() : 去除字符串两边的空格str.lstrip() : 去除字符串左边的空格s ...
- Python关于去除字符串中空格的方法
Python关于去除字符串中空格的方法 在编写程序时我们经常会遇到需要将字符串中的空格去掉的情况,通常我们可以使用下面几种解决方法: 1.strip()方法:该方法只能把字符串头和尾的空格去掉,但是不 ...
- python基础——4(数字、字符串、列表类型的内置方法介绍)
目录 一.可变与不可变类型 二.数字类型 三.字符串类型 四.列表类型 一.可变与不可变类型 可变类型:值改变,但是id不变,证明就是在改变原值,是可变类型 不可变类型:值改变,id也跟着改变,证明产 ...
- python strip()方法使用
描述 python strip() ,用于去除述字符串头尾指定字符(默认为空格或换行符)或字符序列. 注意:此方法只能去除头尾的空格或是换行符,不能去除中间的. 语法: str.strip([char ...
- Python中的数据类型常见方法
list() 方法 1.cmp(list1, list2):#比较两个列表的元素 2.len(list):#列表元素个数 3.max(list):#返回列表元素最大值 4.min(list):#返回列 ...
随机推荐
- linux shell写入单行、多行内容到文件
1.单行文本 #! /bin/bashecho 'hello world' > filename.txt 2.多行文本 代码1: #! /bin/bashcat>filename.txt& ...
- 修改machine.config遇到System.Net.ServicePointManager 的类型初始值设定项引发异常
<system.net>节点应该在</configuration>上面添加,即config页尾. 而不是在<configuration> 后面添加. 在</s ...
- 找进程的窗口Handle
Process[] ProcessList = Process.GetProcessesByName("mspaint");//画图板 IntPtr test = ProcessL ...
- 安装使用git
阿斯蒂芬 http://www.cnblogs.com/alex3714/articles/5930846.html
- 【java】之类加载机制
类加载: 类加载机制是指.class文件加载到jvm,并形成Class对象的机制,之后的应用就可对Class对象进行实例化并调用,类加载机制可在运行时动态加载外部的类,远程网络下载过来的class文件 ...
- 【spring boot】使用@Value映射properties文件属性
描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...
- 前端安全之CSRF
一.跨站点请求伪造(CSRF) 什么是csrf呢? 借助用户的身份去做有损用户利益(一些事情)的事情. 怎么实现跨站点请求伪造呢? 1.伪造者通过创造一个带有<a href=&qu ...
- mina2的processor
processor顾名思义,就是进行IO处理,处理当前session的数据读写,并进行业务处理. 在mina server初始化的时候,会初始化一个processor池,通过NioSocketAcce ...
- springboot+dubbo+tomcat部署出错问题
刚用springboot+dubbo有点不熟悉,部署的时候出现了问题 1 The APR based Apache Tomcat Native library which allows optimal ...
- Ajax的总结
1.运行Ajax的环境,在服务器上才可以实现他的功能,客户端等别的地方,虽然也可以运行,但是功能一定是不全的,有可能很多东西都不会发生反应: 2.传参 (只写关键步骤) (必须在服务器上运行) ge ...