Python内置数据类型之String

Python内置数据类型之String

导论

什么叫字符串?字符串就是一个个字符组成的有序的序列,是字符的集合,在python中通常使用单引号、双引号和三引号引住的字符序列

字符串属性

字符串是不可变对象

区别于列表类型的对象,列表对象时可变的,而字符串对象是不可变的,因此字符串对象不存在通过索引改变其中的字符

字符串支持使用索引访问

字符串可以通过下标索引进行访问

字符串是有序的字符集合,字符序列

字符串在内存中可以是连续的地址空间,有序的序列

字符串是可迭代的

1
2
3
sql = "select * from user where name='tom'"
for i in sql:
print(i,end = ' ')
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
2
3
s1 = 'www.baidu.com'
a,b,c = s1.split('.',maxsplit=2)
a,b,c
('www', 'baidu', 'com')

2. str.partition(sep) -> (head, sep, tail):

将字符串按照分隔符分割成两段,返回这两段和分隔符的元组  
  从左至右,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾三部分的三元组;如果
  没有找到分隔符,就返回头、 2个空元素的三元组
  sep 分割字符串, 必须指定
1
2
3
s2 = 'www.qq.com'
a,b,c = s2.partition('.')
a,b,c
('www', '.', 'qq.com')

字符串大小写

str.upper(): 全大写

str.lower(): 全小写

str.swapcase(): 交换大小写

1
2
3
4
s3 = 'aBCDef'
print(s3.upper())
print(s3.lower())
print(s3.swapcase())
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
2
for i in range(1,14,3):
print('{}'.format(i).zfill(4))
0001
0004
0007
0010
0013

str.ljust(width[, fillchar]) -> str 左对齐

1
2
print('abcd'.ljust(10))
print('abcd'.ljust(10,'0'))
abcd      
abcd000000

str.rjust(width[, fillchar]) -> str 左对齐

1
2
print('abcd'.rjust(10))
print('abcd'.rjust(10,'0'))
      abcd
000000abcd

字符串修改

str.replace(old, new[, count]) -> str

字符串中找到匹配替换为新子串,返回新字符串  
count表示替换几次,不指定就是全部替换  
1
2
print( 'www.baidu.com'.replace('w','p') )
print( 'www.baidu.com'.replace('w','p',2) )
ppp.baidu.com
ppw.baidu.com

strip([chars]) -> str

从字符串两端去除指定的字符集chars中的所有字符
如果chars没有指定,去除两端的空白字符
1
2
print('  abcd123 \r \n \t  '.strip())
print('****abcder***'.strip('*'))
abcd123
abcder

str.lstrip([chars]) -> str 从左开始

str.rstrip([chars]) -> str 从右开始

1
2
print("***abcdef###".lstrip("*"))
print("***abcdef###".rstrip("#"))
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
2
3
4
5
6
7
s = "I am very very very sorry"
print(s.find('very'))
print(s.find('very',6))
print(s.find('very',6,10))
print(s.rfind('very',10))
print(s.rfind('very',10,15))
print(s.rfind('very',-10,-1))
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
2
3
4
5
6
7
s = "I am very very very sorry"
print(s.index('very'))
print(s.index('very', 5))
# print(s.index('very', 6, 13))
print(s.rindex('very', 10))
print(s.rindex('very', 10, 15))
print(s.rindex('very',-10,-1))
5
5
15
10
15

str.count(sub[, start[, end]]) -> int

在指定的区间[start, end),从左至右,统计子串sub出现的次数

1
2
3
4
s = "I am very very very sorry"
print(s.count('very'))
print(s.count('very', 5))
print(s.count('very', 10, 14))
3
3
1

len(string)

返回字符串的长度,即字符的个数

1
2
s = "I am very very very sorry"
print(len(s))
25

字符串判断*

str.endswith(suffix[, start[, end]]) -> bool

在指定的区间[start, end),字符串是否是suffix结尾

str.startswith(prefix[, start[, end]]) -> bool

在指定的区间[start, end),字符串是否是prefix开头
1
2
3
4
5
6
7
8
s = "I am very very very sorry"
print(s.startswith('very'))
print(s.startswith('very',5))
print(s.startswith('very',5,9))
print(s.endswith('sorry'))
print(s.endswith('sorry',5))
print(s.endswith('sorry',5,100))
print(s.endswith('sorry',5,-1))
False
True
True
True
True
True
False

字符串判断 is系列

isalnum() -> bool 是否是字母和数字组成

1
2
print('  '.isalnum())
print('abc123'.isalnum())
False
True

isalpha() 是否是字母

1
2
print("abcdAVAS".isalpha())
print('abcd12'.isalpha())
True
False

isdecimal() 是否只包含十进制数字

1
2
print('1234'.isdecimal())
print('abc123'.isdecimal())
True
False

isdigit() 是否全部数字(0~9)

1
2
print('12345'.isdigit())
print('12345abcd'.isdigit())
True
False

isidentifier() 是不是字母和下划线开头,其他都是字母、数字、 下划线

1
2
print('_abcd124'.isidentifier())
print('12abcd124'.isidentifier())
True
False

islower() 是否都是小写

1
2
print("abcd".islower())
print('ABcd'.islower())
True
False

isupper() 是否全部大写

1
2
print('ABCD'.isupper())
print('ABCde'.isupper())
True
False

isspace() 是否只包含空白字符

1
2
print(' \r \n \t '.isspace())
print(' 11 '.isspace())
True
False

字符串格式化

字符串的格式化是一种拼接字符串输出样式的手段,更灵活方便
    join拼接只能使用分隔符,且要求被拼接的是可迭代对象且其元素是字符串
    拼接字符串还算方便,但是非字符串需要先转换为字符串才能拼接

format函数格式字符串语法

“{} {xxx}”.format(*args, kwargs) -> str**

对齐

1
2
3
4
5
print('{0} * {1} = {2:<2}'.format(3,2,2*3))
print('{0} * {1} = {2:<02}'.format(3,2,2*3))
print('{0} * {1} = {2:>02}'.format(3,2,2*3))
print('{:^30}'.format('centered'))
print('{:*^30}'.format('centered'))
3 * 2 = 6 
3 * 2 = 60
3 * 2 = 06
           centered           
***********centered***********

进制

1
2
print('int:{0:d}; hexo:{0:x}; oct:{0:o}; bin:{0:b}'.format(42))
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
2
3
4
5
6
7
8
9
print("{}".format(3**0.5)) # 1.7320508075688772
print("{:f}".format(3**0.5)) # 1.732051,精度默认6
print("{:10f}".format(3**0.5)) # 右对齐,宽度10
print("{:2}".format(102.231)) # 宽度为2
print("{:.2}".format(3**0.5)) # 1.7 2个数字
print("{:.2f}".format(3**0.5)) # 1.73 小数点后2位
print("{:3.2f}".format(3**0.5)) # 1.73 宽度为3,小数点后2位
print("{:3.3f}".format(0.2745)) # 0.275
print("{:3.3%}".format(1/3)) # 33.333%
1.7320508075688772
1.732051
  1.732051
102.231
1.7
1.73
1.73
0.275
33.333%
-------------本文结束感谢您的阅读-------------
0%