1. endswith() startswith()
1 #以什么什么结尾
2 #以什么什么开始
3 test = "alex"
4 v = test.endswith('ex')5 v = test.startswith('ex')6 print(v)
View Code
2. expandtabs()
1 test = "1\t2345678\t9"
2 v = test.expandtabs(6)3 print(v,len(v))
View Code
3. find()
1 #从开始往后找,找到第一个之后,获取其未知
2 #> 或 >=
3 test = "alexalex"
4 #未找到 -1
5 v = test.find('e')6 print(v)
View Code
4. index()
1 #index找不到,报错 忽略
2 test = "alexalex"
3 v = test.index('a')4 print(v)
View Code
5. format() format_map()
1 #格式化,将一个字符串中的占位符替换为指定的值
2 test = 'i am {name}, age {a}'
3 print(test)4 v = test.format(name='alex',a=19)5 print(v)6
7 test = 'i am {0}, age {1}'
8 print(test)9 v = test.format('alex',19)10 print(v)11
12 #格式化,传入的值 {"name": 'alex', "a": 19}
13 test = 'i am {name}, age {a}'
14 v1 = test.format(name='df',a=10)15 print(v1)16 v2 = test.format_map({"name": 'alex', "a": 19})17 print(v2)
View Code
6. isalnum()
1 #字符串中是否只包含 字母和数字
2 test = "er;"
3 v =test.isalnum()4 print(v)
View Code
7.capitalize() 首字母大写
1 test = "sqy"
2 v =test.capitalize()3 print(v)
View Code
8. casefold() lower() 所有变小写,casefold更牛逼,很多未知的对相应变小写
1 test = "sqy"
2 v1 =test.casefold()3 print(v1)4 v2 =test.lower()5 print(v2)
View Code
9. center() 置宽度,并将内容居中
1 # 20代指总长度2 # *空白未知填充,一个字符,可有可无3 test = "sqy"
4 v = test.center(20,"中")5 print(v)
View Code
10. ljust() rjust() zfill()
1 # test = "alex"
2 # v = test.ljust(20,"*")3 # print(v)4
5 # test = "alex"
6 # v = test.rjust(20,"*")7 # print(v)8
9 # test = "alex"
10 # v = test.zfill(20)11 # print(v)
View Code
11. count() 去字符串中寻找,寻找子序列的出现次数
1 test = "aLexalexr"
2 v = test.count('ex')3 print(v)4
5 test = "aLexalexr"
6 v = test.count('ex',5,6)7 print(v)
View Code
12. isalpha() 是否是字母,汉子
1 test = "as大多数df"
2 v =test.isalpha()3 print(v)
View Code
13. isdecimal() isdigit() isnumeric() 当前输入是否是数字
1 test = "②" # 1,②,儿2 v1 =test.isdecimal()3 v2 =test.isdigit()4 v3 =test.isnumeric()5 print(v1,v2,v3)
View Code
14.isprintable() 是否存在不可显示的字符
1 # \t 制表符2 # \n 换行3 test = "gdds\tfdsfd"
4 v =test.isprintable()5 print(v)
View Code
15. isspace() 判断是否全部是空格
1 test = " ass"
2 v =test.isspace()3 print(v)
View Code
16. istitle() title() 判断是否是标题
1 test = "Return True if all cased characters in S are uppercase and there is"
2 v1 =test.istitle()3 print(v1)4 v2 =test.title()5 print(v2)6 v3 =v2.istitle()7 print(v3)
View Code
17. join() 将字符串中的每一个元素按照指定分隔符进行拼接
1 test = "你是风儿我是沙"
2 print(test)3 t = ' '
4 v = "_".join(test)5 print(v)
View Code
18. islower()lower() isupper() upper()
1 test = "Alex"
2 v1 =test.islower()3 v2 =test.lower()4 print(v1, v2)5
6 v1 =test.isupper()7 v2 =test.upper()8 print(v1,v2)
View Code
19. lstrip() rstrip() strip() 移除指定字符串 有限最多匹配
1 test = "\nxas12xa\n\txax1221axa "
2 v = test.lstrip('xa')3 v = test.rstrip('9lexxexa')4 v = test.strip('xa')5 print(v)6
7 test.lstrip()8 test.rstrip()9 test.strip()10 # 去除左右空白11 v =test.lstrip()12 v =test.rstrip()13 v =test.strip()14 print(v)15 print(test)16 # 去除\t \n17 v =test.lstrip()18 v =test.rstrip()19 v =test.strip()20 print(v)
View Code
20. maketrans() translate() 对应关系替换
1 v = "asidufkasd;fiuadkf;adfkjalsdjf"
2 m = str.maketrans("aeiou", "12345")3 new_v =v.translate(m)4 print(new_v)
View Code
21. partition() rpartition() 分割为三部分
1 test = "testasdsddfg"
2 v = test.partition('s')3 print(v)4 v = test.rpartition('s')5 print(v)
View Code
22. split() rsplit() splitlines()
1 test = "sqys"
2 v = test.split('s',2)3 print(v)4 v =test.rsplit()5 print(v)6
7
8 # 23 分割,只能根据,true,false:是否保留换行9 # test = "asdfadfasdf\nasdfasdf\nadfasdf"
10 # v =test.splitlines(False)11 # print(v)
View Code
23. swapcase() 大小写转换
1 test = "aLex"
2 v =test.swapcase()3 print(v)
View Code
24. isidentifier() 字母,数字,下划线 : 标识符 def class
1 a = "de12_f"
2 v =a.isidentifier()3 print(v)
View Code
25.replace() 将指定字符串替换为指定字符串
1 test = "alexalexalex"
2 v = test.replace("ex",'bbb')3 print(v)4 v = test.replace("ex",'bbb',2)5 print(v)
View Code
26.灰魔法
一、for循环
1 test = "返回到合肥东方红刚才修改"
2 #for 变量名 in 字符串:
3 #变量名
4 #break
5 #continue
6
7
8 index =09 while index
13 index += 1
14 print('=======')15
16 for zjw intest:17 print(zjw)18
19 test = "好方法个地方个地方刚发的"
20 for item intest:21 print(item)22 break
23
24 for item intest:25 continue
26 print(item)
View Code
二、索引,下标,获取字符串中的某一个字符
1 test = "好方法个地方个地方刚发的"
2 v = test[3]3 print(v)
View Code
三、切片
1 test = "好方法个地方个地方刚发的"
2 v = test[0:10] #0=< <1
3 print(v)
View Code
四、获取长度
1 #Python3: len获取当前字符串中由几个字符组成
2 test = "好方法个地方个地方刚发的"
3 v =len(test)4 print(v)
View Code
27. range() 获取连续或不连续的数字
1 #Python2中直接创建在内容中
2 #python3中只有for循环时,才一个一个创建
3 v = range(10)4 v = range(3,10)5 v = range(1,10,2)6 #帮助创建连续的数字,通过设置步长来指定不连续
7 v = range(0, 100, 5)8
9 for item inv:10 print(item)
View Code
28.zip()
1 #print(list(zip(('a','n','c'),(1,2,3))))
2 #print(list(zip(('a','n','c'),(1,2,3,4))))
3 #print(list(zip(('a','n','c','d'),(1,2,3))))
4 #5 #p={'name':'alex','age':18,'gender':'none'}
6 #print(list(zip(p.keys(),p.values())))
7 #print(list(p.keys()))
8 #print(list(p.values()))
9 #10 print(list(zip(['a','b','c'],'12345')))
View Code