一、字符串
1.1 简述
一个个字符组成的有序的序列,是字符的集合
使用单引号、双引号、三引号引住的字符序列
字符串是 不可变 对象
Python 3 开始,字符串就是 Unicode 类型
1.2 字符串定义 初始化
示例
s1 = 'string'
s2 = "string2"
s3 = '''this's a "String" '''
s4 = 'hello \n rookie.com'
s5 = r"hello \n rookie.com"
s6 = 'c:\windows\nt'
s7 = R"c:\windows\nt"
s8 = 'c:\windows\\nt'
sql = """select * from user where name='tom'"""
示例.png
1.3 字符串元素访问 —— 下标
字符串支持使用索引访问
a) 示例
sql = "select * from user where name='tom'"
sql[4] # 字符串 'c'
sql[4] = 'o' # 常量,不可修改
示例.png
有序的字符集合,字符序列
a) 示例
for c in sql:
print(c)
print(type(c))
示例.png
可迭代
a) 示例
lst = list(sql)
示例.png
二、字符串连接
2.1 join 连接用法
'string'.join(iterable) -> str
a) 将可迭代对象连接起来,使用 string 作为分隔符
b) 可迭代对象本身元素都是字符串
c) 返回一个新字符串
2.2 示例
lst = ['1','2','3']
print("\"".join(lst)) # 分隔符为 "
print(" ".join(lst))
print("\n".join(lst))
lst = ['1',['a','b'],'3'] # 不可执行,中间 ['a','b'] 类型为 list 不为 str
print(" ".join(lst))
示例.png
2.3 + 连接用法
+ -> str
a) 将 2 个字符串连接在一起
b) 返回一个新字符串
2.4 示例
a = 'x' + 'y'
print(a)
示例.png
+= *= 示例.png
三、字符串分割
3.1 分割字符串的方法分为 2 类
split 系
a) 将字符串按照分隔符分割成若干字符串,并返回列表
partition 系
a) 将字符串按照分隔符分割成 2 段,返回这 2 段和分隔符的元组
3.2 split
用法
split(sep=None,maxsplit=-1) -> list of strings
a) 从左至右
b) seq 指定分割字符串,缺省的情况下空白字符串作为分割符
c) maxsplit 指定分割的次数,-1 表示遍历整个字符串
示例
# 分隔符
a = ",".join(map(str, range(5)))
a.split(',')
'a b \n \t \r\n c'.split() # 默认使用空白字符,贪婪匹配
'a b \n \t \r\n c'.split(' ')
# 最大切割数
a = ",".join(map(str, range(5)))
a.split(',')
'a b \n \t \r\n c'.split(maxsplit=1) # 指定切割几次
分隔符示例.png
最大切割数示例.png
反向切割示例.png
3.3 splitlines
用法
splitlines([keepends]) -> list of strings
a) 按照行来切分字符串
b) keepends 指定是是否保留行分隔符
c) 行分隔符包括 \n、\r\n、\r 等
示例
'a \t b\nc d\re f\nghi\r\nok'.splitlines()
'a \t b\nc d\re f\nghi\r\nok'.splitlines(True) # 保留分隔符
splitlines 示例.png
3.4 partition
用法
partition(sep) -> (head, sep, tail)
a) 从左至右,遇到分隔符就把字符串分割成两部分,返回 头、分隔符、尾 三部分的三元组;如果没有找到分隔符,就返回 头、2 个空元素 的三元组
b) sep 分割字符串,必须指定
示例
print('a,b,c'.partition(','))
print('a,b,c'.partition(' '))
partition 示例.png
四、字符串大小写
4.1 upper()
全大写
upper 示例.png
4.2 lower()
全小写
lower 示例.png
4.3 大小写,做判断的时候用
4.4 swapcase()
交换大小写
swapcase 示例.png
五、字符串排版
5.1 title() -> str
标题的每个单词都大写
title 示例.png
5.2 capitalize() -> str
收个单词大写
capitalize 示例.png
5.3 center(width[, fillchar]) -> str
width 打印宽度
fillchar 填充的字符 (只能指定一个字符)
center 示例.png
5.4 zfill(width) -> str
-width 打印宽度,居右,左边用 0 填充
zfill 示例.png
5.5 ljust(width[, fillchar]) -> str
左对齐
ljust 示例.png
5.6 rjust(width[, fillchar]) -> str
右对齐
rjust 示例.png
六、字符串修改
6.1 replace(old,new[,count]) -> str
字符串中找到匹配替换为新子串,返回新字符串
count 表示替换几次,不指定就是全部替换
示例
'www.rookie.com'.replace('w','p')
'www.rookie.com'.replace('w','p',2)
'www.rookie.com'.replace('w','p',3)
'www.rookie.com'.replace('ww','p',2)
'www.rookie.com'.replace('www','python',2)
replace 示例.png
6.2 strip([chars]) -> str
从字符串两端去除指定的字符集 chars 中的所有字符
如果 chars 没有指定,去除两端的空白字符
示例
s = "\r \n \t Hello Python \n \t"
s.strip()
s = " I am very very sorry "
s.strip('Iy')
s.strip('Iy ')
strip 示例.png
6.3 lstrip([chars]) -> str
从左开始
lstrip 示例.png
6.4 rstrip([chars]) -> str
从右开始
rstrip 示例.png
七、字符串查找
7.1 find(sub[, start[, end]]) -> int
在指定的区间 [start, end),从左至右,查找子串 sub,找到返回索引,没找到返回 -1
示例
s = 'I am very very sorry'
s.find('very')
s.find('very',5)
s.find('very',6,13)
find 示例.png
7.2 rfind(sub[, start[, end]]) -> int
在指定的区间 [start, end),从右至左,查找子串 sub,找到返回索引,没找到返回 -1
示例
s = 'I am very very sorry'
s.rfind('very',10)
s.rfind('very',10,15)
s.find('very',-10,-1)
rfind 示例.png
7.3 index(sub,[, start[, end]]) -> int
在指定的区间 [start, end),从左至右,查找子串 sub,找到返回索引,没找到抛出异常 ValueError
示例
s = 'I am very very sorry'
s.index('very')
s.index('very',5)
s.index('very',6,13)
index 示例.png
7.4 rindex(sub[, start[, end]]) -> int
在指定的区间 [start , end),从右至左,查找子串 sub,找到返回索引,没找到抛出异常 ValueError
示例
s = 'I am very very sorry'
s.rindex('very',10)
s.rindex('very',10,15)
s.rindex('very',-10,-1)
rindex 示例.png
7.5 count(sub[, start[, end]]) -> int
在指定的区间 [start, end),从左至右,统计子串 sub 出现的次数
示例
s = 'I am very very very sorry'
s.count('very')
s.count('very',5)
s.count('very',10,14)
count 示例.png
7.6 时间复杂度
index 和 count 方法都是 O(n)
随着列表数据规模的增大,效率下降
7.7 len(string)
返回字符串的长度,即字符的个数
八、字符串判断
8.1 endswith(suffix[, start[, end]]) -> bool
在指定的区间 [start, end),字符串是否是 suffix 结尾
示例
s = 'I am very very very sorry'
s.startswith('very')
s.startswith('very',5)
s.startswith('very',5,9)
startswith 示例.png
8.2 startswith(prefix[, start[, end]]) -> bool
在指定的区间 [start, end],字符串是否是 prefix 开头
示例
s = 'I am very very very sorry'
s.endswith('very',5,9)
s.endswith('very',5)
s.endswith('very',5,-1)
s.endswith('very',5,100)
endswith 示例.png
8.3 is 系列
isalnum() -> bool
是否是字母和数字组成
示例
s1 = 'abc123'
s1.isalnum()
isalpha()
是否是字母
示例
s1 = 'abc'
s1.isalpha()
isdecimal()
是否只包含十进制数字
示例
s1 = '123'
s1.isdecimal()
isdecimal 示例.png
isdigit()
是否全部数字(0-9)
示例
s1 = '123'
s1.isdigit()
isdigit 示例.png
isidentifier()
是否字母和下划线开头,其他都是字母、数字、下划线
示例
'abc'.isidentifier()
'_abc'.isidentifier()
'1abc'.isidentifier()
'abc1'.isidentifier()
'abc_'.isidentifier()
'abc-'.isidentifier()
isidentifier 示例.png
islower()
是否都是小写
示例
‘abc'.islower()
isupper()
是否都是大写
示例
'ABC'.isupper
isspace()
是否只包含空白字符
示例
' '.isspace()
'\t'.isspace()
isspace 示例.png