python基础-基本数据类型总结_整型(int)_字符型(str)_day3
一、基本数据类型
1、整型(int)
ps1:
数字 int ,所有的功能,都放在int里
a1 = 123
a1 = 456
ps2:
int 将字符串转换为数字
1 # -int 2 # 将字符串转换为数字 3 a = "123" 4 b = int(a) 5 b = b + 1000 6 print(b)
执行结果:
1 1123
ps3:
这种类型,不能转换
1 a = "123a" #不能转换,会报错 2 b = int(a) 3 print(b)
执行结果:
1 Traceback (most recent call last):
2 File "D:/python/day3/s4.py", line 14, in <module>
3 b = int(a)
4 ValueError: invalid literal for int() with base 10: '123a
ps4:
type 查看他是什么数据类型
1 a = "123" 2 print(type(a)) 3 b = int(a) 4 print(type(b))
执行结果:
1 <class 'str'> 2 <class 'int'>
ps5:
将字符串转换为数字
1 a = "123" 2 print(type(a),a) 3 4 b = int(a) 5 print(type(b),b)
执行结果:
1 <class 'str'> 123 2 <class 'int'> 123
ps6:
字符串以二进制的方式进行转换
1 num = "0011" #这个字符串以二进制的方式进行转换 2 v = int(num, base=2) 3 print(v)
执行结果:
3
ps7:
字符串以十六进制的方式进行转换
1 num = "a" #这个字符串以十六进制的方式进行转换 2 v = int(num, base=16) 3 print(v)
执行结果:
1 10
ps8:
字符串以十六进制的方式进行转换
1 num = "0011" #这个字符串以十六进制的方式进行转换 2 v = int(num, base=16) 3 print(v)
执行结果:
1 17
ps9:
bit_lenght
当前数字的二进制,至少用n位表示
1 # 当前数字的二进制,至少用n位表示 2 # 1 1 3 # 2 10 4 # 3 11 5 # 4 100 6 # 5 101 7 #当前数字的二进制,至少用n位来表示 8 #age = 1 9 #age = 2 10 11 age = 5 12 r = age.bit_length() 13 print(r)
执行结果:
1 3
2、字符串(str)
ps1:
capitalize 首字母大写
1 test = "aLex" 2 v = test.capitalize() 3 print(v)
执行结果:
Alex
ps2:
lower ,casefold
所有变小写,casefold更牛逼,很多未知的对相应变小写
1 test = "aLex" 2 v1 = test.casefold() #casefold更牛逼,很多未知的对相应变小写 3 print(v1) 4 v2 = test.lower() #只能处理普通英文的字符,欧洲的特殊字符处理不了 5 print(v2)
执行结果:
1 alex 2 alex
ps3:
3 设置宽度,并将内容居中
20 代指总长度
* 空白未知填充,一个字符,可有可无
语法:def center(self,width,fillchar=None) #后面接参数:1、直接忽略 2、必须带 3、有等号的是可选项(可带可不带,如果不设置就用默认:None参数)
1、cneter居中 左右两边空格填充(总共长度20位,不够的左右两边空格填充)
test = "aLex" v = test.center(20) print(v)
执行结果:
1 aLex #总长度20位,不够左右两边空格填充
2、cneter居中 左右两边*号填充(总共长度20位,不够的左右两边*号填充)
1 test = "aLex" 2 v = test.center(20,"*") 3 print(v)
执行结果:
1 ********aLex********
3、center居中 左右两边中字填充(总共20位,不够的左右两边中字填充)
1 test = "aLex" 2 v = test.center(20,"中") 3 print(v)
执行结果:
1 中中中中中中中中aLex中中中中中中中中
4、ljust 右边以*填充(总共20位)
1 test = "alex" 2 v = test.ljust(20,"*") 3 print(v)
执行结果:
1 alex****************
5、rjust 左边以*填充 (总共20位)
1 test = "alex" 2 v = test.rjust(20,"*") 3 print(v)
执行结果:
1 ****************alex
6、zfill 左边以0填充(总共20位)
1 test = "alex" 2 v = test.zfill(20) 3 print(v)
执行结果:
1 0000000000000000alex
ps4:
1、count 去字符串中寻找,寻找子序列的出现次数
1 test = "aLexalexr" 2 v = test.count('ex') #计算ex 出现的次数 3 print(v)
执行结果:
1 2
2、count 5,6(表示起始位置,结束位置)
语法:def count(self,sub,start=None,end=None)
1 test = "aLexalexr" 2 v = test.count('ex',5,6) #参数中的5,6表示,从那开始到那结束) 3 print(v)
执行结果:
1 0
ps5:
encode 和 decode (这两个没有讲,先欠着,后面讲到的时候再充补)
1、encode
2、decode
ps6:
1、startswith 以什么什么开始
1 test = "alex" 2 v = test.startswith('ex') 3 print(v)
执行结果:
1 False
2、endswith 以什么什么结尾
1 test = "alex" 2 v = test.endswith('ex') 3 print(v)
执行结果:
1 True
ps7:
1、用法:expandtabs 断句 6
1 # !/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author: nulige 4 5 test = "12345678\t9" #123456:6位 四个空格+78:\t补齐4位+78 9:如果后面没有6位,就不管了 6 v = test.expandtabs(6) #6就是指每6位一段,\t:就是不够六位的用空格补齐 7 print(v,len(v)) #len :判断字符串的长度
执行结果:
1 12345678 9 13
2、expandtabs,断句20,
1 # !/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author: nulige 4 5 # expandtabs,断句20, 6 #输出三个\n 就换行 内容就是输出三行。 7 test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123" 8 v = test.expandtabs(20) 9 print(v)
执行结果:
1 username email password
2 laiying ying@q.com 123
3 laiying ying@q.com 123
4 laiying ying@q.com 123
ps8:
1、find 从开始往后找,找到第一个之后,获取其位置
> 或 >=
未找到 -1
test = "alexalex" v = test.find('ex') #从前向后找,获取其位置 print(v)
执果结果:
1 2
2、index找不到,报错 忽略
1 test = "alexalex" 2 v = test.index('8') 3 print(v)
执行结果:
1 Traceback (most recent call last):
2 File "D:/python/day3/s3.py", line 137, in <module>
3 v = test.index('8')
4 ValueError: substring not found
ps9:
1、format 格式化,将一个字符串中的占位符替换为指定的值
1 test = 'i am {name}, age {a}' 2 v = test.format(name='alex',a=19) 3 print(v)
执行结果:
1 i am alex, age 19
2、用数字,可以直接传值
1 test = 'i am {0}, age {1}' 2 v = test.format('alex',19) 3 print(v)
执行结果:
1 i am alex, age 19
ps10:
format 格式化,传入的值 {"name": 'alex', "a": 19}
1 test = 'i am {name}, age {a}' 2 v1 = test.format(name='df',a=10) 3 v2 = test.format_map({"name": 'alex',"a": 19}) #这两种格式结果是一样的,只是写法不同 4 print(v1) 5 print(v2)
执行结果:
1 i am df, age 10 2 i am alex, age 19
ps11:
isalnum 判断字符串中只能包含字母和数字,如果是就是True
1 test = "jjj123" 2 v = test.isalnum() 3 print(v)
执行结果:
1 True
ps12:
isalpha 判断字符串中是否是字母和汉子(如果不是就是Flase)
1 test = "asdf" 2 v = test.isalpha() 3 print(v)
1 True
ps13:
1、iddecimal ,isdigit,isnumeric 判断当前输入的值,是否是数字
1 test = "②" #这种特殊的数字 2 v1 = test.isdecimal() #判断是否是数字
3 v2 = test.isdigit() #判断是否是数字,还可以判断特殊的数字! 4 print(v1,v2)
执行结果:
1 False True
2、isnumeric 判断当前输入是否是数字
1 test = "二" # 1,② 2 v3 = test.isnumeric() 3 print(v3)
执行结果:
1 True
ps14:
1、isidentifier 判断字母,数字,下划线:标识符:def class
1 a = "_123" 2 v = a.isidentifier() 3 print(v)
执行结果:
1 True
2、isidentifier 判断字母,数字,下划线:标识符:def class
1 a = "def" #标识符也符合 2 v = a.isidentifier() 3 print(v)
执行结果:
1 True
3、isidentifier 判断字母,数字,下划线:标识符:def class
1 a = "123" #不符合,没有下划线 2 v = a.isidentifier() 3 print(v)
执行结果:
1 False
ps15:
isprintable 是否存在不可显示的字符
\t 制表符
\n 换行
1 test = "oiuas\tdfkj" 2 v = test.isprintable() 3 print(v)
执行结果:
1 False
ps16:
isspace 判断是否全部是空格
1 test = " " 2 v = test.isspace() 3 print(v)
执行结果:
1 True
ps17:
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() #再判断v2,就是大写啦。所以是True 7 print(v3)
执行结果:
1 False 2 Return True If All Cased Characters In S Are Uppercase And There Is 3 True
ps18: (标记为五星表示经常用到的)
***** join 将字符串中的每一个元素按照指定分隔符进行拼接
1 test = "你是风儿我是沙" 2 v = "_".join(test) 3 print(v)
执行结果:
1 你_是_风_儿_我_是_沙
ps19:
判断是否全部是大小写 和 转换为大小写
1、islower 判断是否是小写 和 lower转换为小写
1 test = "Alex" 2 v1 = test.islower() #判断是否是小写; 用于验证码:不管用户输入的是大写或小写,统一转换为小写 3 v2 = test.lower() #转换为小写 4 print(v1, v2)
执行结果:
1 False ALEX
2、isupper判断是否是大写和upper 转换为大写
1 test = "Alex" 2 v1 = test.isupper() #判断是否是大写 3 v2 = test.upper() #转换为大写 4 print(v1,v2)
执行结果:
1 False ALEX
ps20:
1、lstrip,rstrip,strip去除左右空白
1 test = " alex " 2 v1 = test.lstrip() #处理左边的空格 3 v2 = test.rstrip() #处理右边的空格 4 v3 = test.strip() #两边都处理空格 5 print(v1,v2,v3)
执行结果:
1 alex alex alex
2、去除\t \n
1 test = "\nlex " 2 v1 = test.lstrip() #处理左边的\n or \t 3 print(test) 4 print(v1)
执行结果:
1 #空格 2 lex 3 lex
3、去除左边的x (移除最多字符)
1 test = "xalex " 2 v1 = test.lstrip('x') #处理左边的x 3 print(v1)
执行结果:
1 alex
4、rstrip 从右边开始往左边找,先进行最多匹配
1 test = "xalex" 2 v1 = test.rstrip('91lexex') #从右边开始往左边找,先进行最多匹配 3 print(v1)
执行结果:
1 xa
ps21:
translate 对应关系替换
1 v = "asidufkasd;fiuadkf;adfkjalsdjf" 2 m = str.maketrans("aeiou", "12345") #定义a=1,e=2,其他依此类推 3 new_v = v.translate(m) 4 print(new_v)
执行结果:
1 1s3d5fk1sd;f351dkf;1dfkj1lsdjf
ps22:
1、partition,rpartition 分割为三部分
1 test = "testasdsddfg" 2 v = test.partition('s') #找到第1个s进行分割,只能分成三部分 3 print(v) 4 5 test = "testasdsddfg" 6 v = test.rpartition('s') 7 print(v)
执行结果:
1 ('te', 's', 'tasdsddfg') #第一个的结果 2 ('testasd', 's', 'ddfg') #第二个的结果
2、split, rsplit 分割为指定个数,分割后s拿不到
- 正则表达表 (以后会学)
- 是否想要分割的元素
- 用途:计算器
1 test = "testasdsddfg" 2 v = test.split('s',2) 3 print(v) 4 5 test = "testasdsddfg" 6 v = test.rsplit('s',2) 7 print(v)
执行结果:
1 ['te', 'ta', 'dsddfg'] 2 ['testa', 'd', 'ddfg']
3、partition 和 split 的区别
答:区别就是分割后一个可以拿到*,一个不能拿到*
1 test = "1*2" 2 v = test.partition('*') 3 print(v) 4 5 test = "1*2" 6 v = test.split('*',2) 7 print(v)
执行结果:
1 ('1', '*', '2') #拿到了* 2 ['1', '2'] #没有拿到*
ps23:
splitlines 分割,只能根据,true,false:是否保留换行
1 test = "asdfadfasdf\nasdfasdf\nadfasdf" 2 v = test.splitlines() 3 print(v) 4 5 test = "asdfadfasdf\nasdfasdf\nadfasdf" 6 v = test.splitlines(True) 7 print(v) 8 9 test = "asdfadfasdf\nasdfasdf\nadfasdf" 10 v = test.splitlines(False) #参数只能加True or False 11 print(v)
执行结果:
1 ['asdfadfasdf', 'asdfasdf', 'adfasdf'] #不加参数 2 ['asdfadfasdf\n', 'asdfasdf\n', 'adfasdf'] #True 3 ['asdfadfasdf', 'asdfasdf', 'adfasdf'] #False
ps24:
1、startswith 判断以xxx开头
1 test = "backend 1.1.1.1" 2 v = test.startswith('b') #xxx开头 3 print(v)
执行结果:
1 True
2、endswith 判断以xxx结尾
1 test = "backend 1.1.1.1" 2 test.endswith('1') #以xxx结尾 3 print(v)
执行结果:
1 True
ps25:
swapcase 大小写转换
1 test = "aLex" 2 v = test.swapcase() 3 print(v)
执行结果:
1 AlEX
ps26:
1、replace 将指定字符串替换为指定字符串
1 test = "alexalexalex" 2 v = test.replace("ex",'bbb') 3 print(v)
执行结果:
1 albbbalbbbalbbb
2、replace 将指定字符串替换为指定字符串,指定个数
1 test = "alexalexalex" 2 v = test.replace("ex",'bbb',2) 3 print(v)
执行结果:
1 albbbalbbbalex
重点:
一、七个基本魔法如下:
- join
- split
- find
- strip
- upper
- lower
- replace
二、4个灰魔法如下:
test = "郑建文妹子有种冲我来"
1、for循环
1 for 变量名 in 字符串: 2 变量名 3 break 4 continue 5 6 index = 0 7 while index < len(test): 8 v = test[index] 9 print(v) 10 11 index += 1 12 print('=======') 13 14 for zjw in test: 15 print(zjw) 16 17 test = "郑建文妹子有种冲我来" 18 for item in test: 19 print(item) 20 break 21 22 for item in test: 23 continue 24 print(item)
2、索引,下标,获取字符串中的某一个字符
1 v = test[3] 2 3 print(v)
3、切片
1 v = test[0:-1] # 0=< <1 2 print(v)
4、获取长度
Python3: len获取当前字符串中由几个字符组成v = len(test)print(v)
1 v = len(test) 2 print(v)
注意:
1 len("asdf") 2 for循环 3 索引 4 切片
5、获取连续或不连续的数字,
1 Python2中直接创建在内容中 2 python3中只有for循环时,才一个一个创建 3 r1 = range(10) 4 r2 = range(1,10) 5 r3 = range(1,10,2) 6 7 8 帮助创建连续的数字,通过设置步长来指定不连续 9 v = range(0, 100, 5) 10 11 for item in v: 12 print(item) 13 14 15 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置 16 test = input(">>>") 17 for item in test: 18 print(item) 19 20 将文字 对应的索引打印出来: 21 test = input(">>>") 22 print(test) # test = qwe test[0] test[1] 23 l = len(test) # l = 3 24 print(l) 25 26 r = range(0,l) # 0,3 27 for item in r: 28 print(item, test[item]) # 0 q,1 w,2 e 29 30 test = input(">>>") 31 for item in range(0, len(test)): 32 print(item, test[item])
三、1个深灰魔法
记住两句话:
- 字符串一旦创建,不可修改
- 一旦修改或者拼接,都会造成重新生成字符串
1 name = "zhengjianwen" 2 age = "18" 3 info = name + age 4 print(info)
执行结果:
1 zhengjianwen18