python(5)字符串处理 (sub,replace,find,index,upper,strip,split,sub翻页
一,sub和replace的用法
re.sub 函数进行以正则表达式为基础的替换工作
re.sub替换到目标字符串中的a,b或者c,并全部替换
另加上sub翻页操作:
re.sub('start=\d+','start=%d'%i,url,re.S)
>>> import re
>>> re.sub('[abc]','o','Mark')
'Mork'
>>> re.sub('[abc]','o','caps')
'oops'
>>
replace 用法介绍:
>>> a
'asds23DFG34'
>>> a.replace('s','M') #全部替换
'aMdM23DFG34'
>>> b = 'adfafafafa'
>>> b.replace('a','M',3) #指定个数的替换
'MdfMfMfafa'
二,find和index的用法
index,find 返回的都是找到的字符串的下标;find如果找不到返回的值 则是-1,而index直接抛出异常
a.find('t',start)从起始位置搜索
a.find('t',start,end)从指定位置开始搜索
a.rfind('t')从右边位置开始搜索
a.count('t') 搜索到多少个指定的字符
>>> a = ''
>>> a.find('')
0
>>> a.find(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object
>>> a.find('')
1
>>> a
''
>>> f = a.find('M')
>>> f
-1
>>> f = a.find('') #返回的是字符串的第一个位置
>>> f
2
>>> f = a.find('')
>>> f
-1
''
>>> a.index('')
2
>>> a.index('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> f = a.index('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
三,大小写处理,upper,lower,swapcase,capitalize,title
>>> a
'123dfsdfs'
>>> a = 'asds23DFG34'
>>> a.upper() #全部大写
'ASDS23DFG34'
>>> a.lower() #全部小写
'asds23dfg34'
>>> a.swapcase() #大小写交换
'ASDS23dfg34'
>>> a.capitalize() #首字母大写其余小写
'Asds23dfg34'
>>> a.title()
'Asds23Dfg34' #子串,首字母大写
>>>
KeyboardInterrupt
四,strip的用法
Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。
这三个函数都可传入一个参数,指定要去除的首尾字符。
需要注意的是,传入的是一个字符数组,编译器去除两端所有相应的字符
>>> a
'asds23DFG34'
>>> a.strip('a')
'sds23DFG34'
>>> a.strip('s')
'asds23DFG34'
>>> s = 'saaaay yes no yaaaass'
>>> s.strip('say') #两边都找
' yes no ' #两边各有一个空格
>>> s.lstrip('say') #只找左边
' yes no yaaaass'
>>> s.rstrip('say') #只找右边
'saaaay yes no '
五,split的用法
Python split()通过指定分隔符对字符串进行切片
语法 str.split('分隔符',num)
- str -- 分隔符,默认为空格。
- num -- 分割次数。
- 返回分割后的字符串列表
- 结果一:把\n归为了换行符,所以直接去掉了
#!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"
print str.split( )
print str.split(' ', 1 ) 以上实例输出结果如下:
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']样例:
>>> str = "asd \dfa \dadf"
>>> str.split()
['asd', '\\dfa', '\\dadf'] #自动增加了换行符
>>> str = "asd \\dfa \\dadf"
>>> str.split()
['asd', '\\dfa', '\\dadf']
>>> aa = str.split()
>>> aa
['asd', '\\dfa', '\\dadf']
>>> aa[]
'asd'
>>> aa[]
'\\dfa'
>>> aa[]
'\\dadf'
>>> bb = r"asdf \bsdf \fsdfe"
>>> bb.split()
['asdf', '\\bsdf', '\\fsdfe']
>>> print aa[] #自动去掉转义
asd
>>> print aa[]
\dfa
>>> print aa[]
\dadf
>>>
python(5)字符串处理 (sub,replace,find,index,upper,strip,split,sub翻页的更多相关文章
- python 替换字符串的方法replace()、正则re.sub()
一.replace()函数1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b 1 2 3 二.re ...
- python判断字符串是否为空的方法s.strip()=='' if not s.strip():
python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='': print 's is null' 或者 if not s.strip(): p ...
- python中字符串拆分与合并——split()、join()、strip()和replace()
Python3 split()方法 描述split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 语法split()方法语法: str.split(str= ...
- Python格式化字符串~转
Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...
- Python格式化字符串
在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的pr ...
- Python中字符串的使用
这篇文章主要介绍python当中用的非常多的一种内置类型——str.它属于python中的Sequnce Type(序列类型).python中一共7种序列类型,分别为str(字符串),unicode( ...
- Python总的字符串
Python总最常用的类型,使用单引号双引号表示.三引号之间的字符串可以跨多行并且可以是原样输出的. Python中不支持字符类型,字符也是字符串. ---字符串的CRUD [1:3] [:6] -- ...
- python之字符串
字符串与文本操作 字符串: Python 2和Python 3最大的差别就在于字符串 Python 2中字符串是byte的有序序列 Python 3中字符串是unicode的有序序列 字符串是不可变的 ...
- python数据类型——字符串类型
字符串(string) 字符串,就是字符连成一串,是由字符组成的序列.字符串有编码问题,在之前我已经讲过.本节主要讲字符串的使用. 创建字符串,不用多说: a='123abcd' b='diamond ...
随机推荐
- JVMInternals
http://blog.jamesdbloom.com/JVMInternals.html http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne ...
- Unity代码热更新方案 JSBinding + SharpKit 首页
目前Unity的代码更新方案有很多,主要以lua为主. JSBinding + SharpKit 是一种新的技术,他做了两件事情: JSBinding将C#导出到 JavaScript (引擎是 Mo ...
- 关于margin和padding的总结
总结一下: 要想实现如(图一)效果,(即一个div中的子元素与父元素有间距): 如果类名为.middle的父元素没有写border,则类名为firstChild的子元素设置margin-top,会导致 ...
- 你足够了解Context吗?
你足够了解Context吗? 这里有关于Context的一切-写在前面: 当我还是一个24K纯Android新手的时候(现在是也是个小Android萌新),拿着工具书对着电脑敲敲打打,那个时候我就有一 ...
- Phython 学习笔记之——类的初步认识
类是面向对象编程的核心,他扮演相关数据及逻辑容器的角色.他们提供了创建实例对象的蓝图.因为python语言不要求必须以面向对象的方式编程(与JAVA不同),这里简单的举一个例子. 如何定义一个类 cl ...
- js字符串函数
JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...
- PHP 文件迭代器
使用了SPL的 迭代器, 可以直接对打开的文件进行foreach读取, 类的构造如下 class fileIterator implements Iterator { private $fp; pri ...
- PHP使用session_set_save_handler陷阱
陷阱如下 当脚本使用了session_set_save_handler 来重定向 session后,使用session_destroy后再使用session_start()重新开启session会报错 ...
- 由浅入深探究mysql索引结构原理、性能分析与优化 转
第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1. 简单介绍B-tree B+ tree树 2. MyisAM索引结构 3. Annode索引结构 4. MyisAM索引与Inno ...
- VS 2010 编译安装 boost 库 -(和 jsoncpp 库共存)
boost库的简单应用很容易,网上有很多资料,但是,如果要json 和 boost 一起使用就会出现这样那样的问题, 有时候提示找不到 “libboost_coroutine-vc100-mt-sgd ...