一、整形
1. base
#在16进制中的位置 num = "b" v = int(num, base=16) print(v) #11
2. bit_length()
# 1 1 # 2 10 # 3 11 # 4 100 # 5 101 # 当前数字的二进制,至少用n位表示 age = 5 r = age.bit_length() print(r) # 3 age = 2 r = age.bit_length() print(r) # 2
二、字符串
需要记住六个基本魔法:join(), split(), find(), strip(), upper(), lower()
1. find
test = "alexalex" v = test.find('lex',3) print(v) 从第3个位置'x'查找'lex'
2. format
test = 'i am {name}, age {a}' print(test) v = test.format(name='alex',a=19) print(v) test = 'i am {0}, age {1}' print(test) v = test.format('alex',19) print(v) #格式化,传入的值 {"name": 'alex', "a": 19} test = 'i am {name}, age {a}' v1 = test.format(name='df',a=10) v2 = test.format_map({"name": 'alex', "a": 19}) print(v1) print(v2)
test = "aLEx" #首字母大写 v = test.capitalize() print(v) #所有字母大写 v2 = test.upper() print(v2) # 所有变小写,casefold更牛逼,很多未知的对相应变小写 v3 = test.casefold() print(v3) v4 = test.lower() print(v4)
4. center
#设置宽度,并将内容居中 # 20 代指总长度 # * 空白未知填充,一个字符,可有可无 test = 'alex' v = test.center(20,"中") print(v) # 中中中中中中中中alex中中中中中中中中
5. count
# 去字符串中寻找,寻找子序列的出现次数 test = "aLexalexr" v = test.count('ex') print(v) # 2 test = "aLexalexr" v = test.count('ex',5,7) print(v) # 0 v = test.count('ex',5,8) print(v) # 1
6. endswith(), startswith()
# 以什么什么结尾 # 以什么什么开始 test = "alex" v1 = test.endswith('ex') print(v1) # False v2 = test.startswith('ex') print(v2) # True
7. isalnum(), isalpha()
# 字符串中是否只包含 字母、数字或汉字 test = "123d好" v1 = test.isalnum() print(v1) # True # 字符串中是否只包含 字母或汉字 v2 = test.isalpha() print(v2) # False
8. isdecimal(), isdigit(), isnumeric()
# test = "二" # 1,② , 是否为数字 test = '12' v1 = test.isdecimal() v2 = test.isdigit() #包含② v3 = test.isnumeric() #包含“二” print(v1,v2,v3) #True True True
9. isprintable()
# 是否存在不可显示的字符 # 制表符 # 换行 test = "oiuas dfkj" v = test.isprintable() print(v) # False
10. isspace()
# 15 判断是否全部是空格 test = " " v = test.isspace() print(v) # True
11. istitle(), title()
# 16 判断是否是标题 test = "Return True if all cased characters in S" v1 = test.istitle() print(v1) # False v2 = test.title() print(v2) # Return True If All Cased Characters In S v3 = v2.istitle() print(v3) # True
12. join()
# 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接 test = "你是风儿我是沙" print(test) t = ' ' v = t.join(test) print(v) # 你 是 风 儿 我 是 沙
13. islower(), isupper()
# 18 判断是否全部是大小写 和 转换为大小写 test = "Alex" v1 = test.islower() v2 = test.lower() print(v1, v2) # False alex v1 = test.isupper() v2 = test.upper() print(v1,v2) # False ALEX
14. lstrip(), rstrip(), strip()
# 移除指定字符串 test = "wxadx" v1 = test.lstrip('x') v2 = test.rstrip('x') v3 = test.strip('x') print(v1,v2,v3) # wxadx wxad wxad # 去除左右空白 v = ' df ' print(v) # ' df ' v1 = v.lstrip() v2 = v.rstrip() v3 = v.strip() print(v1) # 'df ' print(v2) # ' df' print(v3) # 'df'
15. maketrans(), translate()
# 对应关系替换 test = "aeiou" test1 = "12345" v = "asidufkasd;fiuadkf;adfkjalsdjf" m = str.maketrans("aeiou", "12345") new_v = v.translate(m) print(new_v) # 1s3d5fk1sd;f351dkf;1dfkj1lsdjf
16. partition(), rpartition()
# 21 分割为三部分 test = "testasdsddfg" v = test.partition('s') print(v) # ('te', 's', 'tasdsddfg') v = test.rpartition('s') print(v) # ('testasd', 's', 'ddfg')
17. split(), rsplit()
# 分割为指定个数 test = "testasdsddfg" v1 = test.split('s',2) print(v1) # ['te', 'ta', 'dsddfg'] v2 = test.rsplit('s',2) print(v2) # ['testa', 'd', 'ddfg']
18. splitlines()
# 分割,只能根据,true,false:是否保留换行 test = "asdfadfasdf asdfasdf adfasdf" v1 = test.splitlines(True) print(v1) # ['asdfadfasdf ', 'asdfasdf ', 'adfasdf'] v2 = test.splitlines(False) print(v2) # ['asdfadfasdf', 'asdfasdf', 'adfasdf']
19. swapcase()
# 大小写转换 test = "aLex" v = test.swapcase() print(v) # AlEX
20. replace()
# 将指定字符串替换为指定字符串 test = "alexalexalex" v = test.replace("ex",'bbb') print(v) #全部替换 v = test.replace("ex",'bbb',2) print(v) #替换前两个
三、4个灰魔法:所有地方都能用
1. for循环
test = "郑建文妹子有种冲我来" for item in test: print(item)
郑
建
文
妹
子
有
种
冲
我
来
2、索引
# 二、索引,下标,获取字符串中的某一个字符 test = '123dg' v = test[3] print(v) # d
3、切片
# 三、切片 test = '12df56' v = test[0:2] print(v) # 12d
4 、获取长度
# 四、获取长度 # Python3: len获取当前字符串中由几个字符组成 test = 'asdfg' v = len(test) print(v) # 5
四、一个深灰魔法
###################### 1个深灰魔法 ######################
# 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会造成重新生成字符串
# name = "zhengjianwen"
# age = "18"
#
# info = name + age
# print(info)