Python 内置数据结构之三(字符串上)

2019-08-19  本文已影响0人  Alexander_Zz

一、字符串

1.1 简述
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 字符串元素访问 —— 下标
sql = "select * from user where name='tom'"
sql[4]   # 字符串 'c'
sql[4] = 'o'   # 常量,不可修改
示例.png
for c in sql:
    print(c)
    print(type(c))
示例.png
lst = list(sql)
示例.png

二、字符串连接

2.1 join 连接用法
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 + 连接用法
2.4 示例
a = 'x' + 'y'
print(a)
示例.png += *= 示例.png

三、字符串分割

3.1 分割字符串的方法分为 2 类
3.2 split
# 分隔符
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
'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
print('a,b,c'.partition(','))
print('a,b,c'.partition(' '))
partition 示例.png

四、字符串大小写

4.1 upper()
4.2 lower()
4.3 大小写,做判断的时候用
4.4 swapcase()

五、字符串排版

5.1 title() -> str
5.2 capitalize() -> str
5.3 center(width[, fillchar]) -> str
5.4 zfill(width) -> str

-width 打印宽度,居右,左边用 0 填充


zfill 示例.png
5.5 ljust(width[, fillchar]) -> str
5.6 rjust(width[, fillchar]) -> str

六、字符串修改

6.1 replace(old,new[,count]) -> str
'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
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
6.4 rstrip([chars]) -> str

七、字符串查找

7.1 find(sub[, start[, end]]) -> int
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
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
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
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
s = 'I am very very very sorry'
s.count('very')
s.count('very',5)
s.count('very',10,14)
count 示例.png
7.6 时间复杂度
7.7 len(string)

八、字符串判断

8.1 endswith(suffix[, start[, end]]) -> bool
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
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 系列
s1 = 'abc123'
s1.isalnum()
s1 = 'abc'
s1.isalpha()
s1 = '123'
s1.isdecimal()
isdecimal 示例.png
s1 = '123'
s1.isdigit()
isdigit 示例.png
'abc'.isidentifier()
'_abc'.isidentifier()
'1abc'.isidentifier()
'abc1'.isidentifier()
'abc_'.isidentifier()
'abc-'.isidentifier()
isidentifier 示例.png
‘abc'.islower()
'ABC'.isupper
' '.isspace()
'\t'.isspace()
isspace 示例.png
上一篇下一篇

猜你喜欢

热点阅读