Python内置数据结构之Str

字符串:

  •   一个个字符组成的有序的序列,是字符的集合
  •   使用单引号、双引号、三引号引住的字符序列
  •   字符串是不可变对象
  •   python3起,字符串就统一为Unicode类型

字符串元素访问–下标:

  • 字符串支持使用索引访问(一个个字符组成的,空格也是字符)
示例:
sql="select * from user where name='tom'"
sql[4] #字符串'c'
sql[4] = 'o'

有序的字符集合,字符序列:

for c in sql:
	print(c)
	print(type(c))
可迭代
	lst=list(sql)

字符串join连接:

(join是字符串对象的方法(把可迭代对象的元素连接起来,返回字符串))

  • “string”.join(iterable)->str
  • 将可迭代对象连接起来,使用string作为分隔符
  • 可迭代对象本身元素都是字符串
  • 返回一个新字符串
示例:
lst = ['1','2','3']
print("\"".join(lst))  #分隔符是双引号
print(" ".join(lst))
print("\n".join(lst))
lsst = ['1',['a','b'],'3']
print(" ".join(lsst))
=====================================================================
test = [str(i) for i in range(10) ]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
print(' '.join(test))
0 1 2 3 4 5 6 7 8 9
BTW
  • map函数将lst中的元素一个一个转换类型
示例:
test2 = []
for i in map(str,test1):
    test2.append(i)
print(test2)
['0', '1', '2', '3', '4']
+ ->str
  • 将2个字符串连接在一起
  • 返回一个新字符串
示例:
'abca'+'asdc'
'abcaasdc'

字符串分割:

分割字符串的方法分为2类:
  1. split系(不支持正则表达式):将字符串按照分隔符分割成若干字符串,并返回列表
  2. partition系:将字符串按照分隔符分割成2段,返回这2段和分隔符的元祖
split(sep=None,maxsplit=-1)->list of strings
  • 从左至右(rsplit是从右至左切)
  • sep指定分割符,缺省的情况下空白字符串作为分隔符
  • maxsplit指定分割的次数,-1表示遍历整个字符串
示例:
s1.split()
["'I'm", 'a', 'super', 'student.']
s1.split('s')
["'I'm\ta ", 'uper ', 'tudent.']
s1.split('super')
["'I'm\ta ", ' student.']
s1.split(' ')
["'I'm\ta", 'super', 'student.']
s1.split(' ',maxsplit=1)
["'I'm\ta", 'super student.']
s1.split('\t',maxsplit=2)
["'I'm", 'a super student.']
splitlines([keepends])->list of strings
  • 按照行来切分字符串
  • keepends指的是是否保留行分隔符
  • 行分割符包括\n,\r\n,\r等
示例:
print('ab c\n\nde fg\rkl\r\n'.splitlines())
['ab c', '', 'de fg', 'kl']

print('ab c\n\nde fg\rkl\r\n'.splitlines(True))
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
==============================================================================
s1='''I'm a super teacher.
Your're a super teacher.'''
print(s1)
print(s1.splitlines())
print(s1.splitlines(True))
I'm a super teacher.
Your're a super teacher.
["I'm a super teacher.", "Your're a super teacher."]
["I'm a super teacher.\n", "Your're a super teacher."]
partition(sep)->(head,sep,tail)
  • 从左至右,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾三部分的三元组;如果没有找到分隔符,就返回头、2个元素的三元组

  • sep分割字符串,必须指定

示例:
s1.partition('s')
("I'm a ", 's', 'uper stuman.')
s1.partition('stu')
("I'm a super ", 'stu', 'man.')
s1.partition(' ')
("I'm", ' ', 'a super stuman.')
s1.partition('abc')
("I'm a super stuman.", '', '')
rpartition(sep)->(head,sep,tail)
  • 从右至左,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾三部分的三元组如果没有找到分隔符,就返回2个空元素和尾的三元组
示例:
s1="I'm a super stuman."
s1.rpartition(' ')
("I'm a super", ' ', 'stuman.')

字符串大小写:

upper()

    全部大写

示例:
s2 = 'abcvasdfaf'
s2.upper()
'ABCVASDFAF'
lower()

    全部小写

示例:
s3 = 'ABCVASDFAF'
s3.lower()
'abcvasdfaf'
swapcase()

    交互大小写

示例:
s4 = "AbCdEf"
s4.swapcase()
'aBcDeF'

字符串修改:

replace(old,new[,count])->str
  • 字符串中找到匹配替换为新子串,返回新字符串
  • count表示替换几次,不指定就是全部替换
示例:
'www.magedu.com'.replace('w','p')
'ppp.magedu.com'

'www.magedu.com'.replace('w','p',2)
'ppw.magedu.com'

'www.magedu.com'.replace('w','p',3)
'ppp.magedu.com'

'www.magedu.com'.replace('ww','p',2)
'pw.magedu.com'

'www.magedu.com'.replace('www','python',2)
'python.magedu.com'
==================================================================
s1 = 'asdfasdfaadsfvavaaa'
s1.replace('a','A')
'AsdfAsdfAAdsfvAvAAA'
s1.replace('a','A',2)
'AsdfAsdfaadsfvavaaa'
s1.replace('aa','HH')
'asdfasdfHHdsfvavHHa'
s1.replace('aa','BB',2)
'asdfasdfBBdsfvavBBa'
strip([chars])->str
  • 从字符串两端去除指定的字符集chars中的所有字符
  • 如果chars没有指定,去除两端的空白字符
示例:
s="\r\n\t Hello Python \n \t"
s.strip()
'Hello Python'
s="I am very very very sorry"
s.strip('Iy')
' am very very very sorr'
lstrip([chars])->str
  • 从左开始
rstrip([chars])->str
  • 从右开始

字符串查找:

find(sub[,start[,end])->int 后面是开区间
  • 在指定的区间[start,end],从左至右,查找子串sub。找到返回索引,没找到返回-1
rfind(sub[,start[,end]])->int
  • 在指定的区间[start,end],从右至左,查找子串sub。找到返回索引,没找到返回-1

  • (find找不到元素后抛出-1,因为抛异常的代价小于index,所以一般找东西都用find)

示例:
s = "I am very very very sorry"
s.find('very') 
5  
s.find('very',6,13) 
-1
s.find('very',-10,-1) 
 15
======================================================
s.rfind('very',10)  
10
s.rfind('very',10,15)  
10

BTW:

enumerate()
lsit(enumerate())
		 [(0, 'I'),
		 (1, ' '),
		 (2, 'a'),
		 (3, 'm'),
		 (4, ' '),
		 (5, 'v'),
		 (6, 'e'),
		 (7, 'r'),
		 (8, 'y'),
		 (9, ' '),
		 (10, 'v'),
		 (11, 'e'),
		 (12, 'r'),
		 (13, 'y'),
		 (14, ' '),
		 (15, 'v'),
		 (16, 'e'),
		 (17, 'r'),
		 (18, 'y'),
		 (19, ' '),
		 (20, 's'),
		 (21, 'o'),
		 (22, 'r'),
		 (23, 'r'),
		 (24, 'y')]
index(sub[,start[,end])->int

    在指定的区间[start,end],从左至右,查找子串sub。找到返回索引,没找到返回-1

rindex(sub[,start[,end]])->int

    在指定的区间[start,end],从右至左,查找子串sub。找到返回索引,没找到返回-1

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)
时间复杂度

    index和count方法都是O(n)
    随着列表数据规模的增大,而效率下降

startswith(prefix[,start[,end]])->bool (查看文件扩展类型对不对比如.conf)

    在指定的区间[start,end),字符串是否是prefix开头

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

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

示例:
s = "I am very very very sorry"
s.startswith('very')  
False
s.startswith('very',5)  
True
s.startswith('very',5,9)
True
======================================================================================
s.endswith('very',5,9) 
True
s.endswith('sorry',5) 
True
s.endswith('sorry',5,-1) 
False
s.endswith('sorry',5,100) 
True
'sorry  '.rstrip().endswith('sorry'0-1)   
False

字符串判断is系列:

  • isalnum()->bool是否是字母和数字组成
  • isalpha()是否是字母
  • isdecimal()是否只包含十进制数字
  • isdigit()是否全部数字(0-9)
  • isidentifier()是不是字母和下划线开头,其他都是字母、数字、下划线
  • islower()是否都是小写
  • isupper()是否全部大写
  • isspace()是否只包含空白字符
示例:
'aad_d'.isalnum()  False
'11a'.isalpha()  False
'111'.isdecimal() True
'1111a'.isdigit() False
'1aa1'.isidentifier() False
'1111a'.islower() True
'AAAB'.isupper() True
' '.isspace() True

字符串格式化(format):

    字符串的格式化是一种拼接字符串输出样式的手段,更灵活方便
    join拼接只能使用分隔符,且要求被拼接的是可迭代对象
    +拼接字符串还算方便,但是非字符串需要先转换为字符串才能拼接
    在2.5版本之前,只能使用printf style风格的print输出
    print-style formatting,来自C语言的printf函数

格式要求:
    占位符:使用%和格式字符组成,例如%s、%d等
    s调用str(),r会调用repr(),所有对象都可以被这两个转换。
    占位符中还可以插入修饰字符,例如%03d表示打印3个位置,不够前门补零。
    format % values,格式字符串和被格式的值之间使用%分隔
    values只能是一个对象,或是一个和格式字符串占位符数目相等的元祖,或一个字典

字符串格式化:
  • format函数格式字符串语法————python鼓励使用
    “{}{xxx}”.format(*args,**kwargs)->str
            args是位置参数,是一个元组
            kwargs是关键字参数,是一个字典
花括号表示占位符:

    {}表示按照顺序匹配位置参数,{n}表示取位置参数索引为n的值
    {xxx}表示在关键字参数中搜索名称一致的
    {{}}表示打印花括号

  • 位置参数
    “{}{}”.format(‘192.168.1.100’,8888),这就是按照位置顺序用位置参数替换前面的格式字符串的占位符中
  • 关键字参数或命名参数
    “{server}{1}{0}”.format(8888,‘192.168.1.100’,server=‘Web Server Info:’),位置参数按照序号匹配,关键字参数按照名词匹配
  • 访问元素
    “{0[0]}.{0[1]}”.format((‘magedu’,‘com’))
  • 对象属性访问
    from collections import namedtuple
    Point = namedtuple(‘Point’,‘x y’)
    p = Point(4,5)
    “{{{0.x}{0,y}}}”.format§
    “{{{},{}}}”.format(p.x,p.y)#一般都建议这么写
对齐:
'{0}*{1}={2:<2}'.format(3,2,2*3)       '3*2=6 ' 
'{0}*{1}={2:<02}'.format(3,2,2*3)      '3*2=60'  补零
'{0}*{1}={2:>02}'.format(3,2,2*3)	   '3*2=06'
'{:^30}'.format('centered')   居中对齐
'{:*^30}'.format('centered')  居中对齐用*填充
进制:
"int:{0:d};hex{0:x};oct:{0:o};bin:{0:b}".format(42)   'int:42;hex2a;oct:52;bin:101010'
"int:{0:d};hex{0:#x};oct:{0:#o};bin:{0:#b}".format(42)  'int:42;hex0x2a;oct:0o52;bin:0b101010'
octets=[192,168,0,1]
'{:02X}{:02X}{:02X}{:02X}'.format(*octets)

print("{}".format(3**0.5))         #1.7320508075688772
print("{:g}".format(3**0.5))       #1.73205
print("{:f}".format(3**0.5))       #1.732051
print("{:<10f}".format(3**0.5))     #1.732051   右对齐  
print("{:2}".format(102.231))      #102.231     宽度为2 ,意思是我给你留2个格子如果多出来就撑破
print("{:.2}".format(3**0.5))      #1.7         精度为2位有效数字
print("{:.2f}".format(3**0.5))     #1.73        精度为f后2位
print("{:3.2f}".format(3**0.5))    #1.73        宽度3,精度为f后2位
print("{:3.3f}".format(0.2745))    #0.275        宽度3,精度为f后3位
print("{:3.3%}".format(1/3))       #33.333%      宽度3,精度为小数点后3位

猜你喜欢

转载自blog.csdn.net/qq_35976427/article/details/88915035