在本博客 《Python字符串系列》 中,将介绍以下内容:
- Python内置的str对象及操作
- 字符串的格式化
- Python中的正则表达式
- re模块
本文将介绍Python内置的 str 类型,列举Python中字符串对象支持的方法,使用这些方法可以实现强大的字符串处理功能。
在Python 2 中,普通字符串与Unicode字符串有着明确的区分,二者都是Python内置的基本类型,例如:
1 2 3 4 | >>> type ( str )
< type 'type' >
>>> type ( unicode )
< type 'type' >
|
str 类型的字符串通常被称为普通字符串(plain string),区别于 unicode 类型的字符串(unicode string):
1 2 3 4 5 6 | >>> s = 'this is a plain string.'
>>> type (s)
< type 'str' >
>>> u = u 'this is a unicode string.'
>>> type (u)
< type 'unicode' >
|
unicode字符串通常以 'u' 开头。
在Python 2中,使用 抽象基类 basestring 判断一个字符串是不是 str 类型或者 unicode 类型的实例,因为二者都是 basestring 的子类。
1 2 3 4 5 6 | >>> issubclass ( str , basestring )
True
>>> issubclass ( unicode , basestring )
True
>>> issubclass ( unicode , str )
False
|
本文将介绍Python字符串的内置方法,这些方法对于 plain string 和 unicode 字符串同样适用,如果执行操作的s是一个plain string,那么返回的字符串也是一个plain string,unicode类似。后续的文章将详细分析 unicode 字符串的特性及编码上的一些特点。
Python中,字符串是不可变的序列,支持:重复、连接、索引和切片等操作,例如:
1 2 3 4 | >>> s * n # 将 s 重复n次
>>> s1 s2 # 将字符串 s1 和 s2 连接
>>> s[ 0 ] # 索引字符串 s1 中的第一个字符
>>> s[ 0 : 2 ] # 返回字符串 s 中的前两个字符
|
这些操作都不会改变参与运算的字符串,除非进行显式地赋值。此外,Python还包括了许多字符串处理的小技巧,如:
- 使用s[::-1]可以翻转整个字符串
- 如果一个字符串全部由数字组成,而开头有0,则使用 int(s) 能够自动除去开头的0,将原来的字符串转成一个有意义的整数。
Python内置了丰富的字符串处理功能
1. 首字母大写
capitalize()
返回s的一份拷贝,并不改变s。如果 s 的首字符是一个字母,则拷贝的首字母将其改成大写,其余所有字母转成小写。
例如:
1 2 3 4 5 6 | >>> 'this is a test string.' .capitalize()
'This is a test string.'
>>> '_this is a test string.' .capitalize() # 开头不是字母,不变
'_this is a test string.'
>>> 'this is A test string.' .capitalize() # 除开头外的其他位置上的字母全转成小写
'This is a test string.'
|
2. 对齐方式
(1)左右对齐 ljust()、rjust()
s.ljust(width[, fillchar])<br>s.rjust(width[, fillchar])
|
返回一个长度为 max(len(s), width) 的字符串,如果 width > len(s),则左/右对齐,并在另一端填充 fillchar
例如:
1 2 3 4 5 6 | >>> '1234' .rjust( 8 , '#' )
'####1234'
>>> '1234' .ljust( 8 , '#' )
'1234####'
>>> '1234' .ljust( 2 , '#' )
'1234'
|
(2)居中 center()
s.center(n, fillchar = ' ' )
|
返回一个新的字符串,新字符串的长度为 max(len(s), n),当 n > len(s)时,使用参数 fillchar (默认为空格)填充新字符串中其余的位置,并将 s 置于新字符串的中部。
例如:
1 2 3 4 5 6 7 8 | >>> 'test' .center( 3 )
'test'
>>> 'test' .center( 5 )
' test'
>>> 'test' .center( 6 , '#' )
'#test#'
>>> 'test' .center( 7 , '~' )
'~~test~'
|
可见当左右无法均衡填充时,优先填充左侧。
3. 计数
count()
s.count(sub, start = 0 , end = sys.maxint)
|
统计 s[start:end] 中,子串 sub 出现的次数。
4. str 与 unicode 的转换
(1)str到unicode——decode()
1 | S.decode([encoding[,errors]])
|
使用 decode() 函数可以将 str 类型的plain string 转换成 unicode 类型的字符串,
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> s = '你好'
>>> u = s.decode( 'gbk' )
>>> type (s)
< type 'str' >
>>> type (u)
< type 'unicode' >
>>> print s
你好
>>> print u
你好
>>> s
'\xc4\xe3\xba\xc3'
>>> u
u '\u4f60\u597d'
>>> len (s)
4
>>> len (u)
2
|
(2)Unicode 到 str——encode()
S.encode([encoding[,errors]])
|
使用encode()则可以将 unicode 字符串 转换成 str 类型的 plain string。
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> u = u '你好'
>>> s = u.encode( 'gbk' )
>>> type (u)
< type 'unicode' >
>>> type (s)
< type 'str' >
>>> u
u '\u4f60\u597d'
>>> s
'\xc4\xe3\xba\xc3'
>>> print u
你好
>>> print s
你好
>>> len (u)
2
>>> len (s)
4
|
5. 前后缀
endswith()、startswith()
1 | S.endswith(suffix[, start[, end]])<br>s.startswith(prefix[, start[, end]])
|
返回 bool 型结果,
判断 s[start:end]是否以 suffix 结尾。
6. 扩展制表符
expandtabs()
tabsize默认为8,返回一个 s 的拷贝,其中的“\t”都被扩展成 tabsize 个空格。
例如:
1 2 | >>> 'test\ttest' .expandtabs()
'test test'
|
7. 定位
(1)定位不到时返回 -1 find()、rfind()
1 | s.find(sub [,start [,end]])<br>s.rfind(sub [,start [,end]])
|
返回 int 型结果,表示 sub 在 s[start:end] 中第一次出现的下标。如果在 s[start:end] 中没有找到子串 sub,则返回 -1。
例如:
1 2 3 4 5 6 7 8 | >>> 'testtest' .find( 'est' )
1
>>> 'testtest' .find( 'tt' )
3
>>> 'testtest' .find( 'tt' , 3 )
3
>>> 'testtest' .find( 'tt' , 4 )
- 1
|
(2)定位不到时抛出异常 index()、rindex()
1 | S.index(sub [,start [,end]])<br>s.rindex(sub [,start [,end]])
|
功能与 find() 类似,但是如果没有找到 sub ,则抛出 ValueError。
例如:
1 2 3 4 | >>> 'hello this world' .index( '$' )
Traceback (most recent call last):
File '<stdin>' , line 1 , in <module>
ValueError: substring not found
|
8.
format()
1 | S. format ( * args, * * kwargs)
|
返回 字符串 型结果,
9. 内容形式判断
isalnum()、isalpha()、isdigit()、isspace()
返回布尔型结果,
判断 s 是不是非空,且全部由 字母 和 数字 组成。
返回布尔型结果,
判断 s 是不是非空,且全部由 字母 组成。
返回布尔型结果,
判断 s 是不是非空,且全部由 数字 字符组成。
例如:
1 2 3 4 | >>> '123' .isdigit()
True
>>> '123.456' .isdigit()
False
|
如果 len(s) > 0,且其中的所有字符都是空格,则返回True;
如果 s 为空,或s中存在至少一个非空格的字符,则返回False。
10. 大小写
(1)小写 islower()、lower()
返回布尔型结果,
如果 s 中不含一个小写字母,或至少含有一个大写字母,则返回False,否则返回True,包含其他字符并不影响。
例如:
1 2 3 4 5 6 7 8 9 10 11 12 | >>> '123.456' .islower()
False
>>> 'abcde' .islower()
True
>>> 'abcde$' .islower()
True
>>> 'abcde#%^%' .islower()
True
>>> 'abcdeF' .islower()
False
>>> 'a.213214$#@^%$@' .islower()
True
|
(2)大写 isupper()、upper()
如果 s 中包含的所有字母都是大写,则返回 True
如果s 中不包含字母,或者至少包含一个小写字母,则返回False。
例:
1 2 3 4 5 6 | >>> 'ABC$@' .isupper()
True
>>> 'ASDFGq' .isupper()
False
>>> ''.isupper()
False
|
(3)交换大小写 swapcase()
11. 'titlecase'
istitle()、title()
判断一个字符串是不是“titlecase”:每个独立的连续字母段都以大写字母开头。
例如:
1 2 3 4 5 6 7 8 | >>> 'A Title' .istitle()
True
>>> 'a Title' .istitle()
False
>>> '123 this is a string' .istitle()
False
>>> 'This Is a String' .istitle()
False
|
12. 连接
join()
以 s 为分隔符连接 iterable 中的字符串
例如:
1 2 | >>> '$' .join([ 'hello' , 'this' , 'world' ])
'hello$this$world'
|
13. 拆分
(1)保留分隔符的一次拆分 partition()、rpartition()
s.partition(sep)<br>s.rpartition(sep)
|
以 sep 为分隔符拆分 s ,返回 (head, sep, tail) 形式的三元组。
例如:
1 2 3 4 | >>> 'hello$this$world' .partition( '$' )
( 'hello' , '$' , 'this$world' )
>>> 'hello$this$world' .rpartition( '$' )
( 'hello$this' , '$' , 'world' )<br>>>> 'hello this world' .partition( '$' )<br>(' ', ' ', ' hello this world')
|
(2)不保留分隔符的完全拆分 split()、rsplit()、splitlines()
s.split([chars])<br>s.rsplit([sep [,maxsplit]])<br>s.splitlines(keepends = False )
|
例如:
1 2 | >>> 'hello$this$world' .split( '$' )
[ 'hello' , 'this' , 'world' ]
|
14.
lstrip()、strip()、rstrip()
1 2 3 | s.lstrip([chars]) #从开头删除
s.strip([chars]) # 左右两端同时删除
s.rstrip([chars]) # 从结尾删除
|
16. 替换
replace()
s.replace(old, new[, count])
|
18.
translate()
s.translate(table [,deletechars])
|
19.
zfill()
|