第4章 字符串与正则表达
4.0 编码标准
- 最早的字符串编码是美因标准信息交换码ASCII
4.1 字符串
- Python中字符串
- 属于序列类型
- 支持序列通用方法和切片操作
- 支持特有的字符串操作方法
- 字符串属于不可变序列类型
- Python字符串驻留机制
- 短字符串赋给多个对象时,内存中只有一个副本
- 长字符串不遵守驻留机制
- 判断Python字符串类型
- 可以使用type()和instance()方法
4.1.1 字符串格式化
- 格式化方法
- chr() :unicode转换为char
- ord() :char转换为unicode
>>> chr(ord("3")+1)
'4'
>>> ord("3")
51
>>> ord("a")
97
>>> chr(97)
'a'
- 案例
# 进制间转换
>>> x = 1235
>>> so = "%o" %x
>>> so
'2323'
>>> sh = "%x" %x
>>> sh
'4d3'
>>> se = "%e" %x
>>> se
'1.235000e+03'
>>> chr(ord("3")+1)
'4'
>>> ord("3")
51
>>> ord("a")
97
>>> chr(97)
'a'
>>>
>>> "%s %65"
'%s %65'
>>> "%s" %65
'65'
>>> '%d,%c'%(65,65)
'65,A'
>>> '%d,%c'%(97,97)
'97,a'
>>> '%d'%555
'555'
>>> '%d'%'555'
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
'%d'%'555'
TypeError: %d format: a number is required, not str
>>> '%s'%[1,2,3]
'[1, 2, 3]'
>>>
- 使用format方法进行格式化
- ‘{index:格式化方式}’.format(带入的字符)
def format():
print("The number {0:,} in hex is : {0:#x}, the number {1} in oct is {1:#o}".format(5555, 55))
# The number 5,555 in hex is : 0x15b3, the number 55 in oct is 0o67
print("The number {1:,} in hex is : {1:#x}, the number {0} in oct is {0:#o}".format(5555, 55))
# The number 55 in hex is : 0x37, the number 5555 in oct is 0o12663
# 把元组中的元素作为列表中的一项,二维调用
positon = (5, 8, 13)
print("X:{0[0]};Y:{0[1]};Z:{0[2]}".format(positon))
# X:5;Y:8;Z:13
4.1.2 字符串常用方法
- dir(“”):查看所有字符串操作函数列表
- help()查看每个函数帮助
- find(),rfind(): 查找一个字符串在另一个字符串指定范围(默认是整个字符串)中首次和最后一次出现的位置,如果不存在则返回-1.
- index(),rindex()
- count()
- split(),rsplit()
- partition(),rpartition()
- 字符串连接join()
- lower(),upper(),capitalize(),title(),swapcase()
- replace()
- maketrans(),translate()
- strip(),rstrip(),lstrip()
- eval()
- 关键字in
- startswith(),endswith()
- isalnum(),isalpha(),isdigit(),isspace(),isupper(),islower()
- center(),ljust(),rjust()
4.1.3 字符串常量
-
string.digits
-
string.punctuation
-
string.letters
-
string.printable
-
string.lowercase
-
string.uppercase
-
random库
- random.getranbits(17)
- random.choice()
- random.randrange()
- random.randint()
- random.shuffle(list)
可变字符串
4.2 正则表达式
- 正则表达式
- 字符串处理的有力工具和技术
- 正则表达式原理
- 使用预定义模式去匹配一类具有共同特征的字符串
- 处理字符串:快速、正确地完成复杂的查找、替换等
- Python中re模块提供了正则表达式操作所需要的功能。
4.2.1 正则表达式元字符
- 普通字符串可以匹配自身
4.2.2 re模块的主要方法(要重视这几个函数,参数,返回值,功能;判断时字符串内置还是re模块的方法)
- compile()
- search()
4.2.3 直接使用re模块方法
- 一般re模块方法的第一个是由通配符组成的模式串。
import re
text = 'alpha. beta....gamma delta'
re.split('[\.]+',text)
# ['alpha', 'beta', 'gamma', 'delta']