不同点
###1、符号格式
区别 | 符号 | 实例 |
---|---|---|
字符串 | 单引号,双引号,三单引号,三双引号 | >>> area = ‘Gongshu’ >>> history = “5000” >>> school = “”“浙大,城院,树大”"" >>> river = ‘’‘陈家河,运河,西湖’’’ |
列表 | [] | li = [] li = [1, 2, 3, ‘abcd’, ‘city’, ‘collage’, [“I”, ‘love’, ‘you’]] |
元组 | () | tp =() tp = (1,) tp = (‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘a’, ‘b’, ‘c’, (‘d’, ‘e’, ‘f’)) |
2、增删改查
(1)字符串
类别 | 函数 | 意思 | 实例 |
---|---|---|---|
增 | join() | 将指定字符插入到元素中间 | list = [“I”, “Love”, “Python”] print(’ '.join(list)) I Love Python |
删 | sr.strip() sr.lstrip() sr.rstrip() | 移除字符串头尾指定的字符 移除字符串左边指定的字符 移除字符串右边指定的字符 | sr = “####Life is short,you need python.####“ print(sr.strip(”#”)) print(sr.lstrip("#")) print(sr.rstrip("#")) Life is short,you need python. Life is short,you need python.#### ####Life is short,you need python. |
改 | sr.lower() sr.upper() sr.swapcase() sr.title() sr.capitalize() | 转小写 转大写 大小写互换 转为标题的形式 首字母大写 | sr = "Life is short,you need python." print(sr.lower()) print(sr.upper()) print(sr.swapcase()) print(sr.title()) print(sr.capitalize()) life is short,you need python. LIFE IS SHORT,YOU NEED PYTHON. lIFE IS SHORT,YOU NEED PYTHON. Life Is Short,You Need Python. Life is short,you need python. |
sr.center() sr.ljust() sr.rjust() sr.zfill() | 居中对齐 左对齐 右对齐 右对齐,默认填充0 | sr = "Life is short,you need python." print(sr.center(55,’#’)) print(sr.ljust(41, ‘#’)) print(sr.rjust(41, ‘#’)) print(sr.zfill(41)) ######Life is short,you need python.##### Life is short,you need python.########### ###########Life is short,you need python. 00000000000Life is short,you need python. | |
sr.replace() | 把字符串中旧字符串替换成新字符串,可指定第几位参数 | sr = "Life is short,you need python." print(sr.replace(‘t’,‘T’,1)) Life is shorT,you need python. | |
split() partition() | 以指定字符分割字符串并去除该字符 以指定字符分割字符串并保留该字符 | sr = "Life is short,you need python." print(sr.split(‘o’,2)) print(sr.partition(‘o’)) [‘Life is sh’, ‘rt,y’, ‘u need python.’] (‘Life is sh’, ‘o’, ‘rt,you need python.’) | |
ord() chr() | 字符转数字 数字转字符 | sr = '城市学院’ li = [] for i in sr: li.append(ord(i)) print(li) [22478, 24066, 23398, 38498] | |
查 | sr.count() | 计数 | sr = "Life is short,you need python." print(sr.count(‘e’,0,17)) |
sr.find() sr.index() sr.rindex() | 查找元素并返回第一次出现的元素的索引值;查找不到,返回-1 查找元素并返回第一次出现的元素的索引值;查找不到,报错 从右往左查找 | sr = "Life is short,you need python." print(sr.find(‘z’,19,25)) print(sr.index(‘z’,19,25)) print(sr.rindex(‘e’)) -1 报错 20 |
(2)列表
类别 | 函数 | 意思 | 实例 |
---|---|---|---|
增 | append() | 向列表里追加一个元素 | li = [“City”, “College”] li1 = [‘a’, ‘b’, ‘c’] li.append(li1) print(li) [‘City’, ‘College’, [‘a’, ‘b’, ‘c’]] |
extend() | 向列表里追加另一个列表的所有元素 | li = [“City”, “College”] li1 = [‘a’, ‘b’, ‘c’] li.extend(li1) print(li) [‘City’, ‘College’, ‘a’, ‘b’, ‘c’] | |
insert() | 指定下标添加一个元素 | li = [“City”, “College”] li1 = [‘a’, ‘b’, ‘c’] li.insert(1,li1) print(li) [‘City’, [‘a’, ‘b’, ‘c’], ‘College’] | |
改 | li = [‘City’, ‘College’, ‘a’, ‘b’, ‘c’] li[4]='6666’ print(li) [‘City’, ‘College’, ‘a’, ‘b’, ‘6666’] | ||
reverse() sort() copy() | 将列表元素取反 True降序 False升序 复制列表 | li = [‘a’, ‘b’, ‘c’, ‘d’] li.reverse() print(li) [‘d’, ‘c’, ‘b’, ‘a’] li = [4, 2, 5, 9, 1] li.sort(reverse=True) print(li) li.sort(reverse=False) print(li) [9, 5, 4, 2, 1] [1, 2, 4, 5, 9] li = [‘City’, ‘College’, ‘a’, ‘b’, ‘c’] li1 = li.copy() print(li1) [‘City’, ‘College’, ‘a’, ‘b’, ‘c’] | |
删 | remove() | 删除第一次遇到的指定元素 | li = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’,‘f’,‘e’] li.remove(‘e’) print(li) [‘a’, ‘b’, ‘c’, ‘d’, ‘f’, ‘e’] |
del | 删除列表释放空间 | li = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’,‘f’,‘e’] del li print(li) 报错 | |
clear() | 清空列表里所有元素 | li = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’,‘f’,‘e’] li.clear() print(li) [] | |
pop() | 通过下标删除,如果不指定索引,默认删除最后一个元素,删除指定索引对应的元素 | li = [‘a’,‘b’,‘c’,‘d’,‘e’] li.pop(2) print(li) [‘a’, ‘b’, ‘d’, ‘e’] | |
查 | index() | 用于从列表中找出某个值第一个匹配项的索引位置 | li = [‘City’, ‘College’, ‘a’, ‘b’, ‘c’] print(li.index(‘a’)) 2 |
(4)元组
类别 | 函数 | 意思 | 实例 |
---|---|---|---|
增 | 不可增 | ||
删 | del | 不能删除某个元素,但可以全部删除 | tp = (‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘a’, ‘b’, ‘c’, (‘d’, ‘e’, ‘f’)) del tp print(tp) |
改 | 不可改 | ||
查 | index() | 索引查 | tp = (‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘a’, ‘b’, ‘c’, (‘d’, ‘e’, ‘f’)) print(tp.index(‘a’)) print(tp[8].index(‘d’)) 5 0 |
切片查 | tp = (‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘a’, ‘b’, ‘c’, (‘d’, ‘e’, ‘f’)) print(tp[2]) print(tp[-2]) print(tp[1::2]) 3 c (‘2’, ‘4’, ‘a’, ‘c’) |
共同点
###1、可拼接
类别 | 实例 |
---|---|
字符串 | >>> a = “Hello” >>> b = “,Python” >>> print(a+b) Hello,Python |
列表 | li1 = [‘I’] li2 = [“love”, ‘you’] print(li1 + li2) [‘I’, ‘love’, ‘you’] |
元组 | a = (‘I’,) b = (‘love’, ‘learning’) c = a + b print© (‘I’, ‘love’, ‘learning’) |
2、可重复
类别 | 实例 |
---|---|
字符串 | >>> a = “City College” >>> print(a*3) City CollegeCity CollegeCity College |
列表 | li1 = [‘I’] li2 = [“love”, ‘you’] print(li2*3) [‘love’, ‘you’, ‘love’, ‘you’, ‘love’, ‘you’] |
元组 | a = (‘I’,) b = (‘love’, ‘learning’) c = a * 3 print© (‘I’, ‘I’, ‘I’) |
3、可强转
类别 | 实例 |
---|---|
字符串 | sr = 'abcd’ li = list(sr) print(li, type(li)) [‘a’, ‘b’, ‘c’, ‘d’] <class ‘list’> |
列表 | li = [‘zxcv’] sr = str(li) print(li,type(sr)) [‘zxcv’] <class ‘str’> |
元组 | tp = (123,) li = list(tp) print(tp, type(li)) (123,) <class ‘list’> |
4、可索引
类别 | 实例 |
---|---|
字符串 | >>> sr = “Python” >>> print(len(sr)) 6 >>> print(sr[5]) n |
列表 | li = [‘c’, ‘i’, ‘t’, ‘y’, ‘city’, ‘college’, ‘zhejiang’] print(len(li)) print(li[0], li[-7]) 8 c c |
元组 | tp = (‘a’, ‘b’, ‘c’) print(len(tp)) print(tp[2]) 3 c |
###5、可切片
类别 | 实例 |
---|---|
字符串 | sr = "Life is short,you need python." print(sr[1:19:2]) iei hr,o |
列表 | li = [‘c’, ‘i’, ‘t’, ‘y’, ‘city’, ‘college’, ‘zhejiang’] print(li[:5]) print(li[::-1]) [‘c’, ‘i’, ‘t’, ‘y’, ‘city’] [‘zhejiang’, ‘college’, ‘city’, ‘y’, ‘t’, ‘i’, ‘c’] |
元组 | tp = (‘a’, ‘c’, ‘f’, ‘j’, ‘k,’) print(tp[::3]) print(tp[:2]) (‘a’, ‘j’) (‘a’, ‘c’) |