序列之一:字符串 (python)
字符串类型是Python中最常见的类型。Python中单引号和双引号的作用是相同的。Python使用“原始字符串”操作符来创建直接量字符串。
字符串是一种标量或者直接量。Python解释器在处理字符串时,将其作为单一值。字符串是不可变类型,改变一个字符串的元素需要新建一个字符串。字符串是由独立的字符组成,并且这些字符可以通过切片操作顺序地访问。
1操作符
1.1序列操作符切片[]和[:]
0 1 2 3 正向索引,开始于0,结束于len(str)-1
a b c d
-4 -3 -2 -1 反向索引,开始于-1,结束于-len(str)
eg:
>>> str='abcd'
>>> str[0]
'a'
>>> str[3]
'd'
>>> str[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> str[-1]
'd'
>>> str[-4]
'a'
>>> str[-5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> str[1:5]
'bcd'
>>> str[5:1:-1]
'dc'
>>> str[-1:-5:-2]
'db'
>>> str[-1:-5]
''
>>> str[-5:-1]
'abc'
>>> str[:]
'abcd'
1.2成员操作符 in,not in
成员操作符用于判断一个字符或者一个子串是否出现在另一个字符传中。
>>> 'bm' in 'abcd'
False
>>> 'b' in 'abcd'
True
>>> 'bd' in 'abcd'
False
>>> 'bc' in 'abcd'
True
>>> 'bm' not in 'abcd'
True
>>> 'bc' not in 'abcd'
False
判断一个字符串是否包含另一个字符串,可以由find(),index()或者rfind()和rindex()来完成。
1.3连接符+
通过连接符可以从原有字符串获得一个新的字符串。出于性能考虑,推荐使用%,或者join方法来实现连接。
>>> 'li'+' '+"wang"
'li wang'
>>> 'li'+' '+"wang"[1:3]
'li an'
>>> '%s %s' % ('li','wang'[1:3])
'li an'
>>> ''.join(('li','wang','lala'))
'liwanglala'
1.4格式化操作符%
格式化字符 转换方式
%c 转换成字符
%r 优先使用repr()函数进行字符串转换
%s 优先使用str()函数进行字符串转换
%d/%i 转成有符号的十进制
%u 转换成无符号的十进制
%o 转换成无符号的八进制
%x/%X 转换成无符号的十六进制
%e/%E 转换成科学技术发
%f/%F 转换成浮点形
%% 输出%
对应的格式字符串
* 定义宽度或者小数精度
- 用作左对齐
+ 在正数前面显示空格
<sp> 在正数前面显示空格
# 在八进制前面显示'0',在十六制前显示0x/0X
0 显示的数字前面填充0
m.n m显示的最小总宽度,n是小数点后的位数
Python支持两种形式的输入参数,一种是元组,一种是字典形式。
>>> 'there are %(howmany)d %(lang)s Quotation Symbols'%{'lang':'Python','howmany':4}
'there are 4 Python Quotation Symbols'
>>> 'there are %d %s Quotation Symbols'%(4,'Python')
'there are 4 Python Quotation Symbols'
字符串模板
>>> from string import Template
>>> s = Template('there are ${howmany} ${lang} Quotation Symbols')
>>> s
<string.Template object at 0x100992790>
>>> print s.substitute(lang='Python',howmany=4)
there are 4 Python Quotation Symbols
2string预定义字符
String模块在Python1.6中添加进来。现在已经没有必要导入string模块了,除非要访问该模块自己定义的字符串常量。 Public module variables:
>>>import string
>>>string.xxx
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
octdigits = '01234567'
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
whitespace = '\t\n\x0b\x0c\r '
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
3内建函数
3.1标准函数
cmp()
len()
max() and min()
enumerate()
str() and unicode()
chr() unichr() and ord()
3.2字符串内建函数
capitailize,center,
count
decode(encoding='UTF-8',errors='strict')
encode(encodeing='UTF-8',errors='strict')
endswith,startwith
find,index,
isalnum,isalpha,isdecimal,isdigit,islower,isnumeric,isspace,istitle,isupper
join,replace,split,strip,upper,lower
...
4与字符有关的模块 ##
string 字符串操作函数工具
re 正则表达式
struct 字符串和二进制之间转化
StringIO 字符串缓冲对象
base64 Base 16,32,64编码
codecs 解码器注册和基类
crypt 进行单方面加密
difflib 找出不同序列的不同
hashlib 多种不同安全哈希算法和信息摘要算法
hma HMCA信息鉴权算法
md5 RSA的MD5信息摘要鉴权
rotor 提供多种平台的加解密算法
sha NIAT的安全哈希算法SHA
stringprep 提供用于IP协议的Unicode字符串
textwrap 文本包装和填充
unicodedata Unicode数据库