Python内置数据类型之String
导论
什么叫字符串?字符串就是一个个字符组成的有序的序列,是字符的集合,在python中通常使用单引号、双引号和三引号引住的字符序列
字符串属性
字符串是不可变对象
区别于列表类型的对象,列表对象时可变的,而字符串对象是不可变的,因此字符串对象不存在通过索引改变其中的字符
字符串支持使用索引访问
字符串可以通过下标索引进行访问
字符串是有序的字符集合,字符序列
字符串在内存中可以是连续的地址空间,有序的序列
字符串是可迭代的
1 | sql = "select * from user where name='tom'" |
s e l e c t * f r o m u s e r w h e r e n a m e = ' t o m '
字符串的操作
str.join(iterable) –>str
1. 将可迭代对象连接起来,使用string作为分隔符
2. 可迭代对象本身元素都是字符串
3. 返回一个新的字符串
1 | print( ''.join(['a','b','c']) ) |
abc
+ 号连接
将两个字符串连接在一起
代码格式如下:
1 | print('abc' + 'def') |
abcdef
字符串分割
分割字符串的两种方法分为2类
1. str.split(sep=None, maxsplit=-1) -> list of strings
将字符串安装分隔符分割成若干字符串,并返回列表
从左至右
sep 指定分割字符串,缺省的情况下空白字符串作为分隔符
maxsplit 指定分割的次数, -1 表示遍历整个字符串
1 | s1 = 'www.baidu.com' |
('www', 'baidu', 'com')
2. str.partition(sep) -> (head, sep, tail):
将字符串按照分隔符分割成两段,返回这两段和分隔符的元组
从左至右,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾三部分的三元组;如果
没有找到分隔符,就返回头、 2个空元素的三元组
sep 分割字符串, 必须指定
1 | s2 = 'www.qq.com' |
('www', '.', 'qq.com')
字符串大小写
str.upper(): 全大写
str.lower(): 全小写
str.swapcase(): 交换大小写
1 | s3 = 'aBCDef' |
ABCDEF
abcdef
AbcdEF
字符串排版
str.title() -> str
标题的每个单词都大写
1 | print('hello world'.title()) |
Hello World
str.capitalize() -> str
首个单词大写
1 | print('abc def '.capitalize()) |
Abc def
str.center(width[, fillchar]) -> str
width 打印宽度
fillchar 填充的字符
1 | print(' abcd '.center(20,'*')) |
******* abcd *******
str.zfill(width) -> str
width 打印宽度,居右,左边用0填充
1 | for i in range(1,14,3): |
0001
0004
0007
0010
0013
str.ljust(width[, fillchar]) -> str 左对齐
1 | print('abcd'.ljust(10)) |
abcd
abcd000000
str.rjust(width[, fillchar]) -> str 左对齐
1 | print('abcd'.rjust(10)) |
abcd
000000abcd
字符串修改
str.replace(old, new[, count]) -> str
字符串中找到匹配替换为新子串,返回新字符串
count表示替换几次,不指定就是全部替换
1 | print( 'www.baidu.com'.replace('w','p') ) |
ppp.baidu.com
ppw.baidu.com
strip([chars]) -> str
从字符串两端去除指定的字符集chars中的所有字符
如果chars没有指定,去除两端的空白字符
1 | print(' abcd123 \r \n \t '.strip()) |
abcd123
abcder
str.lstrip([chars]) -> str 从左开始
str.rstrip([chars]) -> str 从右开始
1 | print("***abcdef###".lstrip("*")) |
abcdef###
***abcdef
字符串查找
时间复杂度
index和count方法都是O(n)
随着数据规模的增大,而效率下降
str.find(sub[, start[, end]]) -> int
在指定的区间[start, end),从左至右,查找子串sub。找到返回索引,没找到返回-1
str.rfind(sub[, start[, end]]) -> int
在指定的区间[start, end),从右至左,查找子串sub。找到返回索引,没找到返回-1
1 | s = "I am very very very sorry" |
5
10
-1
15
10
15
str.index(sub[, start[, end]]) -> int
在指定的区间[start, end),从左至右,查找子串sub。找到返回索引,没找到抛出异常ValueError
str.rindex(sub[, start[, end]]) -> int
在指定的区间[start, end),从左至右,查找子串sub。找到返回索引,没找到抛出异常ValueError
1 | s = "I am very very very sorry" |
5
5
15
10
15
str.count(sub[, start[, end]]) -> int
在指定的区间[start, end),从左至右,统计子串sub出现的次数
1 | s = "I am very very very sorry" |
3
3
1
len(string)
返回字符串的长度,即字符的个数
1 | s = "I am very very very sorry" |
25
字符串判断*
str.endswith(suffix[, start[, end]]) -> bool
在指定的区间[start, end),字符串是否是suffix结尾
str.startswith(prefix[, start[, end]]) -> bool
在指定的区间[start, end),字符串是否是prefix开头
1 | s = "I am very very very sorry" |
False
True
True
True
True
True
False
字符串判断 is系列
isalnum() -> bool 是否是字母和数字组成
1 | print(' '.isalnum()) |
False
True
isalpha() 是否是字母
1 | print("abcdAVAS".isalpha()) |
True
False
isdecimal() 是否只包含十进制数字
1 | print('1234'.isdecimal()) |
True
False
isdigit() 是否全部数字(0~9)
1 | print('12345'.isdigit()) |
True
False
isidentifier() 是不是字母和下划线开头,其他都是字母、数字、 下划线
1 | print('_abcd124'.isidentifier()) |
True
False
islower() 是否都是小写
1 | print("abcd".islower()) |
True
False
isupper() 是否全部大写
1 | print('ABCD'.isupper()) |
True
False
isspace() 是否只包含空白字符
1 | print(' \r \n \t '.isspace()) |
True
False
字符串格式化
字符串的格式化是一种拼接字符串输出样式的手段,更灵活方便
join拼接只能使用分隔符,且要求被拼接的是可迭代对象且其元素是字符串
拼接字符串还算方便,但是非字符串需要先转换为字符串才能拼接
format函数格式字符串语法
“{} {xxx}”.format(*args, kwargs) -> str**
对齐
1 | print('{0} * {1} = {2:<2}'.format(3,2,2*3)) |
3 * 2 = 6
3 * 2 = 60
3 * 2 = 06
centered
***********centered***********
进制
1 | print('int:{0:d}; hexo:{0:x}; oct:{0:o}; bin:{0:b}'.format(42)) |
int:42; hexo:2a; oct:52; bin:101010
int:42 hexo:2a oct:52 bin:101010
浮点数
1 | print("{}".format(3**0.5)) # 1.7320508075688772 |
1.7320508075688772
1.732051
1.732051
102.231
1.7
1.73
1.73
0.275
33.333%