2基本数据类型——数字、字符串
基本数据类型
数字(int, long-python3无均为int)
- int-将字符串转换为数字
a = '123' print (type(a),a) b = int(a) print (type(b),b)
num = '0011'
v = int(num, base=2)
print(v)
base=2/8/16 → 以二进制/八进制/十六进制方式转化为十进制
- bit_length() 当前数字的二进制至少用几位来表示
age = 16
r = age.bit_length()
print(r)
字符串(str)——进入源代码查看方法(输入str,按住Ctrl可查看)- 标绿常用
- capitalize() 首字母大写
test = 'umbrella' v = test.capitalize() print (v)
- casefold() 大写变小写
test = 'UMbrella'
v = test.casefold()
print (v)
- lower() 大写变小写,但仅适用于英文字符
test = 'UMbrella' v = test.lower() print (v)
- center(self, width, fillchar=None) 设置宽度并将内容居中(eg中 20代指总长度,*代表空白中的填充内容-可有可无,只能是一个字符)
test = 'umbrella' v = test.center(20, '*') print (v)
- ljust() 左填充
test = 'umbrella' v = test.ljust(20,'*') print(v)
- rjust() 右填充
test = 'umbrella' v = test.rjust(20,'*') print(v)
- count() 去字符串中寻找子序列的出现次数(eg中 0代表从第几个开始找,5代表找到第几个结束)
test = 'umbrellaxeslxe' v = test.count('ex' ,0,5) print (v)
- endswith() 以什么结尾(eg中 0代表从第几个开始找,5代表找到第几个结束)
test = 'umbrellaxeslxe' v = test.endswith('xe' ,0,5) print (v)
- starswith() 以什么开始(eg中 0代表从第几个开始找,5代表找到第几个结束)
test = 'umbrellaxeslxe' v = test.startswith('xe' ,0,5) print (v)
- find() 从前往后找,找到第一个之后获取其位置就终止不再继续找了(eg中 0代表从第几个开始找,5代表找到第几个结束 前一个为开区间后一个为闭区间即≥0 <5的位置里找)
test = 'umbrellaexeslxex' v = test.find('xe' ,0,5) print (v)
- index() 功能同find,但找不到直接报错,很少用一般直接用find
test = 'umbrella' v = test.index('6') print (v)
- format() 格式化,将一个字符串中的占位符替换为指定的值
test = 'I am (name), age{a}' print(test) v = test. format(name='nmbrella' ,a=20) print(v)
- format_map() 格式化功能同上(运行失败找原因中)
test = 'I am (name), age{a}' print(test) v = test. format_map('name': 'umbrella' ,'a': 20) print(v)
- isalnum() 判断字符串中是否只包含字母和数字
test = 'umbrella666' v = test.isalnum() print(v)
- expandtable() 断句20,可将“\t”前未满20的字符串扩展到相同的20长度,\n代表换行(可以做成类似于对齐表格的效果)
test = 'username\temail\tpassword\numbrella\tyfyqq.com\t123\nclothes\txy@qq.com\t456\n' v = test.expandtabs(20) print(v)
- isalpha() 判断字符串中是否只包含字母、汉字
test = 'umbrella雨伞' v = test.isalpha() print(v)
- isdigit()、isdecimal() 、isnumeric()都可判断字符串中是否只包含数字,其中isdecimal仅支持十进制阿拉伯数字,isdigit支持一些特殊数字字符(如②),isnumeric支持中文数字(如“二”)
test = '②' v1 = test.isdecimal() v2 = test.isdigit()
v3 = test.isnumeric() print(v1,v2,v3)
- isidentifier() 看是否符合命名规则(字母、数字、下划线组成;数字不能开头……)
a = '123' b = 'umbrella' v1 = a.isidentifier() v2 = b.isidentifier() print(v1,v2)
- islower() 判断是否都是小写
test = 'umbrella' v = test.islower() print(v)
- isprintable() 是否存在不可见的字符(比如\t制表符、\n换行等)
test = 'umbrella\taaa' v = test.isprintable() print(v)
- isspace() 空字符串,是否全部都是空格
test = 'umbrella' v = test.isspace() print(v)
- istitle() 判断是不是标题(首字母大写为标题)
test = 'umbrella is my nickname' v1 = test.istitle() print(v1) v2 = test.title() print(v2) v3 = v2.istitle() print(v3)
- join() 将字符串中的每一个元素按照指定分隔符进行拼接(较常用,在其它数据类型中可能也会用到)
test = '我是雨伞你是雨衣' print(test) t = ' ' v1 = t.join(test) print(v1) v2 = '_'.join(test) print(v2)
- lower() 转化成小写(验证码、密码等不区分大小写的地方可以在后台都转化为小写比较)
test = 'Umbrella' v1 = test.islower() v2 = test.lower() v3 = v2.islower() print(v1,v2,v3)
- isupper() 判断是否为大写、upper() 转化为大写
- strip() lstrip() rstrip() 去掉空白(\t、\n、空格、指定字符-优先最多匹配)、去掉左边空白、去掉右边空白
test = ' umbrella\n' v1 = test.strip() v2 = test.lstrip() v3 = test.rstrip()
text = 'umbrella'
v4 = text.strip('um') print(v1,v2,v3,v4)
- partition() rpartition() 找到指定字符并从该处进行分割,如果能找到则分成三部分,指定字符前、指定字符、指定字符后;从右往左找指定字符并从该处进行分割
test = 'umbrella' v1 = test.partition('b') v2 = test.rpartition('b') print(v1,v2)
- split() rsplit() 找到指定字符并进行分割,后面的参数指分成几部分,与上面的不同点是指定字符不输出 (可用在运算顺序上,先乘除后加减……)。ps.正则表达式也可以进行分割-可选定是否需要指定的分割字符(相当于partition和split的合体)
test = 'umbrellabccbaaa'
v1 = test.split('b',3)
v2 = test.rsplit('b',2)
print(v1,v2)
- splitlines() 分割,只能根据True和False决定是否保留换行分割
test = 'umbrella\numbrella\naaa'
v = test.splitlines(False)
v1 = test.splitlines(True)
print(v,v1)
- maketrans() 指定字符间的对应关系,进行替换
test = 'umbrelka'
text = '12345678'
v = 'uwmbprcelqla;umbrelka'
m = str.maketrans('umbrelka','12345678')
v1 = v.translate(m)
print(v1)
- startswith() endswith() 是否以指定字符开头/结尾
test = 'umbrella' v = test.startswith('u') v1 = test.endswith('a') print(v,v1)
- swapcase() 大小写转化
test = 'umbRElla' v = test.swapcase() print(v)
- replace() 替换指定字符串,后面参数代表还第几个,例如“1”代表换第一个
test = 'umbrellaumbrella' v = test.replace('um','yy',1) print(v)
- 其它功能(在其它数据类型中可能也会用到)
[ ] 索引、下标,去获取字符串中的某一个字符
test = 'umbrella' v = test[3] v1 = test[0] print(v,v1)
[ : ] 切片,去获取字符串中某范围的字符,用分号分隔
test = 'umbrella' v = test[0:3] v1 = test[0:-1] print(v,v1)
len 获取当前字符串由几个字符组成,在其它数据类型中可能也会用到
test = 'umbrella'
v = len(test)
text = '雨伞'
v1 = len(text)
print(v,v1)
综合使用例子
test = '我是雨伞你是谁呀' index = 0 while index<len(test): v = test[index] print(v) index = index + 1 print('finish')
range() 给字符串按顺序编号
Python2中的range立即创建所有对象,Python3在for循环的时候才一个个创建对象,Python2中的xrange同Python3,即在for循环的时候才一个个创建对象。
其中range(0,100,1)指从第一个开始依次多一个直到第100个,也可以反过来range(100,0,-1),即从第100个开始往前到第一个
test = input('>>>') print(test) v = len(test) print(v) r = range(0,3) for item in r: print(item,test[item])
test = input('>>>') print(test) v = len(test) print(v) for item in range(0,len(test)): print(item,test[item])
- tips
字符串一旦在内存里就创建不可修改,如果要修改则会在内存里创建一个新的字符串
for 循环也支持continue和break
浙公网安备 33010602011771号