一 、列表、元组操作
1.列表的操作
list_test = [1,2,3]
增加:list_test.append(4)---> 结果:[1,2,3,4];
删除:list_test.pop(2) / list_test.remove(3) / del list_test[2] --->结果:[1,2];
修改:list_test[0] = 5--->结果:[5,2,3];
查看:x = list_test[1] --->结果:x = 2; y = list_test.index(2)--->结果:y = 1;
插入:list_test.insert(1,7)--->结果:[1,7,2,3]
反转:list_test.reverse()--->结果:[3,2,1]
浅cope(直接量复制的是值,引用复制的是地址):1.import copy list_test1 = copy.copy(list_test) 2. list_test2 = list_test[:] 3.list_test3 = list(list_test);
其他操作:list_test[::2]--->结果[1,3]; z = list_tese.count(2)--->结果:z = 1。
2.元组是特殊的列表,只可以查看不可修改。
二、字符串操作
str_test = "i have a dream" --->
1. print(‘-‘.join(["1","2","3"])) --->结果:1-2-3 <class ‘str‘>;
2. str_test.capitalize()--->结果:"I have a dream";
3. str_test.count(‘a‘)--->结果:3 ;
4. str_test.center(20,‘-‘)--->结果:---i have a dream---;
5. str_test.endwith(‘am‘)--->结果:True;
6. str_test.find(‘have‘)--->结果:2 ;
7. str_test = "i {name} have a {some} dream" --->str_test.format(name="gangzi",some="big")) / name.format_map( {‘name‘:‘gangzi‘,‘some‘:‘big‘} )
8. ‘a123-‘.isalnum()--->False;
9. ‘ab‘.isalpha()--->True;
10. ‘10‘.isdecimal()---->True;
11. ‘10‘.isdigit()--->True;
12. print(‘aa1‘.isidentifier()) --->True # 判断是不是一个合法的标识符(是否符合变量名规则);
13. ‘33.0‘.isnumeric()--->False;
14. str_test.istitle()--->False;
15. print(str_test.isprintable())--->True # tty file,drive file;
16. str_test.isupper()--->False;
17. print(str_test.ljust(30,‘-‘))--->i {name} have a {some} dream--; # 同理 str_test.rjust(30,‘-‘);
18. str_test.lower()--->i {name} have a {some} dream;
19. str_test.upper()--->I {NAME} HAVE A {SOME} DREAM;
20. str_test.lstrip() / str_test.rstrip() / str_test.strip()
21. p = str.maketrans(‘abcdefg‘,‘1234567‘)--->print(str_test.translate(p))--->结果:i {n1m5} h1v5 1 {som5} 4r51m;
22. print(str_test.replace(‘n‘,‘N‘,1))--->结果:i {Name} have a {some} dream;
23. print(str_test.rfind(‘n‘))---->结果:3 ;
24. print(str_test.split(‘\t‘))--->[‘i {name} have a {some} dream‘] <class ‘list‘>;
25. print(str_test.splitlines())--->[‘i {name} have a {some} dream‘] <class ‘list‘>;
26. str_test.swapcase()--->I {NAME} HAVE A {SOME} DREAM;
27. str_test.title()--->I {Name} Have A {Some} Dream;
28. str_test.zfill(30)--->00i {name} have a {some} dream;
三、字典操作
四、集合操作
五、文件操作
六、字符编码与转码
原文地址:https://www.cnblogs.com/gangzi4321/p/10868725.html