一、数字(int)
- int()
将字符串转换为数字
1 a = "123" 2 print(type(a),a) 3 #结果:<class 'str'> 123 4 b = int(a) 5 print(type(b),b) 6 #结果为:<class 'int'> 123
将16进制以10进制转换并输出
1 num = '0011' 2 v = int(num,base=16) 3 print(v) 4 #结果为:17
- bit_lenght()
1 # 当前数字的二进制,至少用n位表示 2 age = 3 3 #3==》11 4 r = age.bit_length() 5 print(r) 6 #结果:2 7 new_age = 4 8 #4==》100 9 new_r = new_age.bit_length() 10 print(new_r) 11 #结果:3
二、字符串(str)
- capitalize()
1 #首字母大写 2 test = 'soLitary' 3 v = test.capitalize() 4 print(v) 5 #结果:Solitary
- casefold()
- lower()
1 #所有变小写,casefold更牛逼,很多未知的对相应变小写 2 test = 'soLitary' 3 v1 = test.casefold() 4 #结果:solitary 5 print(v1) 6 v2 = test.lower() 7 print(v2) 8 #结果:solitary
- center()
- ljust()
- rjust()
- zfill()
1 '''设置宽度,并将内容居中;20 代指总长度;* 空白未知填充,一个字符,可有可无''' 2 test = 'soLitary' 3 v1 = test.center(20,'爱') 4 print(v1) 5 #结果:爱爱爱爱爱爱soLitary爱爱爱爱爱爱 6 v2 = test.ljust(20,'*') 7 print(v2) 8 #结果:soLitary************ 9 v3 = test.rjust(20,'*') 10 print(v3) 11 #结果:************soLitary 12 v4 = test.zfill(20) 13 print(v4) 14 #结果:000000000000soLitary
- count()
1 #去字符串中寻找,寻找子序列的出现次数 2 test = 'soLitary' 3 v1 = test.count('it') 4 print(v1) 5 #结果:1 6 v2 = test.count('it',5,6) 7 #在索引值为5,6里面去找 8 print(v2) 9 #结果:0
- endswith()
- startswith()
1 #以什么什么结尾 2 #以什么什么开始 3 test = 'soLitary' 4 v1 = test.endswith('ry') 5 print(v1) 6 #结果:True 7 v2 = test.startswith('so') 8 print(v2) 9 #结果:True
- expandtabs()
1 #找制表符(/t),断句20 2 test = "username email password laiying ying@q.com 123 laiying ying@q.com 123 laiying ying@q.com 123" 3 v = test.expandtabs(20) 4 print(v) 5 #结果: 6 '''username email password 7 laiying ying@q.com 123 8 laiying ying@q.com 123 9 laiying ying@q.com 123'''
- find()
1 #从开始往后找,找到第一个之后,获取其位置,未找到 -1 2 test = 'soLitary' 3 v = test.find('so') 4 print(v) 5 #结果:0
- index()
1 #index找不到,报错 忽略 2 test = 'soLitary' 3 v = test.index('6') 4 print(v) 5 '''Traceback (most recent call last): 6 File "F:pycharmPyCharm 2017.3.3helperspydevpydev_run_in_console.py", line 53, in run_file 7 pydev_imports.execfile(file, globals, locals) # execute the script 8 File "F:pycharmPyCharm 2017.3.3helperspydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile 9 exec(compile(contents+" ", file, 'exec'), glob, loc) 10 File "F:/pycharm/pycharm_file/test.py", line 2, in <module> 11 v = test.index('6') 12 ValueError: substring not found'''
- format()
1 #格式化,将一个字符串中的占位符替换为指定的值 2 test1 = 'i am {name}, age {a}' 3 print(test1) 4 #结果:i am {name}, age {a} 5 v1 = test1.format(name='soLitary',a=19) 6 print(v1) 7 #结果:i am soLitary, age 19 8 test2 = 'i am {0}, age {1}' 9 print(test2) 10 #结果:i am {0}, age {1} 11 v2 = test2.format('soLitary',19) 12 print(v2) 13 #结果:i am soLitary, age 19 14 15 16 17 18 #格式化,传入的值 {"name": 'alex', "a": 19} 19 test3 = 'i am {name}, age {a}' 20 print(test3) 21 #结果:i am {name}, age {a} 22 v3 = test3.format(name='df',a=10) 23 print(v3) 24 #结果:i am df, age 10 25 v4 = test3.format_map({"name": 'soLitary', "a": 19}) 26 print(v4) 27 #结果:i am soLitary, age 19
- isalnum()
1 #字符串中是否只包含 字母和数字 2 test = '123' 3 v = test.isalnum() 4 print(v) 5 #结果:True
- isalpha()
1 #是否是字母、汉字 2 test = "as2df" 3 v = test.isalpha() 4 print(v) 5 #结果:False
- isdecimal()
- isdigit()
- isnumeric()
1 #当前输入是否是数字,一个比一个牛逼 2 test = "二" 3 v1 = test.isdecimal() 4 v2 = test.isdigit() 5 v3 = test.isnumeric()
- isprintable()
1 #主要看里面有没有/t,/n 2 # 制表符 3 # 换行 4 test = "oiuas dfkj" 5 v = test.isprintable() 6 print(v) 7 #结果:False
- isspace()
1 #判断是否全部是空格 2 test = "" 3 v = test.isspace() 4 print(v) 5 #结果:False
- istitle()
- title()
1 #判断是否是标题 2 test = "Return True if all cased characters in S are uppercase and there is" 3 v1 = test.istitle() 4 print(v1) 5 #结果:False 6 v2 = test.title() 7 print(v2) 8 #结果:Return True If All Cased Characters In S Are Uppercase And There Is 9 v3 = v2.istitle() 10 print(v3) 11 #结果:True
- join()
1 #将字符串中的每一个元素按照指定分隔符进行拼接 2 test = "你是我的小宝贝" 3 print(test) 4 #结果:你是我的小宝贝 5 t = ' ' 6 v = t.join(test) 7 print(v) 8 #结果:你 是 我 的 小 宝 贝
- islower()/lower()
- isupper()/upper()
1 #判断是否全部是大小写 和 转换为大小写 2 test = "soLitary" 3 v1 = test.islower() 4 v2 = test.lower() 5 print(v1, v2) 6 #结果:False solitary 7 v3 = test.isupper() 8 v4 = test.upper() 9 print(v3,v4) 10 #结果:False SOLITARY
- lstrip()
- rstrip()
- strip()
1 #移除指定字符串 2 test = "xa" 3 v1 = test.lstrip('xa') 4 v2 = test.rstrip('9lexxexa') 5 #有限最多匹配,子序列 6 v3 = test.strip('xa') 7 print(v1,v2,v3) 8 #结果:
- translate()
1 #对应关系替换 2 test = "aeiou" 3 test1 = "12345" 4 v = "asidufkasd;fiuadkf;adfkjalsdjf" 5 m = str.maketrans(test, test1) 6 new_v = v.translate(m) 7 print(new_v) 8 #结果:1s3d5fk1sd;f351dkf;1dfkj1lsdjf
- partition()
- rpartition
1 #分割,分割的部分也显示输出 2 test = "testasdsddfg" 3 v1 = test.partition('s') 4 print(v1) 5 #结果:('te', 's', 'tasdsddfg') 6 v2 = test.rpartition('s') 7 print(v2) 8 #结果:('testasd', 's', 'ddfg')
- split()
- rsplit()
1 #分割,分割部分不显示输出 2 test = "testasdsddfg" 3 v1 = test.split('s',2) 4 print(v1) 5 #结果:['te', 'ta', 'dsddfg'] 6 v2 = test.rsplit('s',2) 7 print(v2) 8 #结果:['testa', 'd', 'ddfg']
- splitlines()
1 #分割,只能根据/n 2 test = "asdfadfasdf asdfasdf adfasdf" 3 v = test.splitlines(False) 4 #true,false:是否保留换行 5 print(v) 6 #结果:['asdfadfasdf', 'asdfasdf', 'adfasdf']
- swapcase()
1 #大小写转换 2 test = 'soLiTary' 3 v = test.swapcase() 4 print(v) 5 #结果:SOlItARY
- isidentifier()
1 #判断数字、字母、下划线:标识符 2 a = "de" 3 v = a.isidentifier() 4 print(v) 5 #结果:True
- replace()
1 #将指定字符串替换为指定字符串 2 test = "alexalexalex" 3 v1 = test.replace("ex",'bbb') 4 print(v1) 5 #结果:albbbalbbbalbbb 6 v2 = test.replace("ex",'bbb',2) 7 print(v2) 8 #结果:albbbalbbbalex
索引,下标,获取字符串中的某一个字符
v = test[3]
切片
v = test[0:-1] #0<= <-1
获取长度
v = len(test)
###################### 7个基本魔法 ######################
- join
- split
- find
- strip
- upper
- lower
- replace
###################### 1个深灰魔法 ######################
- 字符串一旦创建,不可修改
- 一旦修改或者拼接,都会造成重新生成字符串
#例: name = "zhengjianwen" age = "18" info = name + age
内存不可能会在原有的字符串内进行修改,那样做会耗费很大内存,它只会去创建一个新的字符串