一、int 整型
主要用于计算
运算符 | 解释 |
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 取余数 |
** | 幂的运算 |
// | 取整,输出商 |
操作:
主要有一种
1 #bit_length() 计算1个十进制占用的二进制的 最少 位数 2 Demo = 8 3 print(Demo.bit_length()) 4 # 4
二、bool 布尔值
bool 布尔值只有2种
True False
str 和 bool 转换
非空字符都是True
空字符是False
1 # str 转 bool 2 print(bool('hello')) 3 print(bool(' ')) #空格 4 print(bool('')) 5 #True 6 #True 7 #False 8 #----------------------------------- 9 #bool 转 str 10 a = str(True) 11 b = str(False) 12 print(a,type(a)) 13 print(b,type(b)) 14 # True <class 'str'> 15 # False <class 'str'>
三、str 字符串
字符串的操作:
3.1、字符串的索引
find(self, sub, start=none, end=none)
对原始字符串中某个字符串进行定位,确定其位置(索引),只返回找到的第一个匹配的字符串的位置,而不管其后是否还有匹配的字符串
1 a = 'tableabbleeee' 2 print(a.find('b')) #只定位第一个b的位置 后面的不管 3 print(a.find('y')) # 找不到匹配的字符串 返回 -1 4 print(a.find('b',3,7)) # 这个不是切片了 是在索引区间找'b' 5 # 2 6 # -1 7 # 6
index(self, sub, start=None, end=None)
index() 和 find() 函数一样,区别就是找不到匹配的字符串, 报错
1 a = 'tableabbleeee' 2 print(a.find('y')) # 找不到匹配的字符串 报错 3 4 # Traceback (most recent call last): 5 # File "D:/Demo/test.py", line 2, in <module> 6 # print(a.index('y')) 7 # ValueError: substring not found
3.2、索引取值与切片
顾头不顾尾
1 name = "aleX leNb" 2 print(name[1]) #取第2位 3 print(name[3]) #取第4位 4 print(name[0:]) #取第1位到最后 5 print(name[:]) #取第1位到最后 6 print(name[2:]) #取第3位到最后 7 print(name[1::2]) #从第2位开始到最后,每间隔1个,取1个 8 print(name[2:6]) #空格也有索引值 取第3位到第6位 索引值2 <= n < 6 9 print(name[5:0:-1]) #倒取值 步长位负值 -1为连续 -3为间隔2位取值 10 # l 11 # X 12 # aleX leNb 13 # aleX leNb 14 # eX leNb 15 # lXlN 16 # eX l 17 # l Xel
1 a = 'abcdefg' 2 print(a[1:-1]) 3 # bcdef
3.3、str.capitalize(self)
首字母大写 其他字母变成小写
1 a = 'tablE' 2 print(a.capitalize()) 3 #Table
1 str = 'alexS' 2 #首字母大写,其他变成小写 3 str1 = str.capitalize() 4 print(str,str1) 5 # alexS Alexs 对字符串操作,会生成一个新字符串str1,原来的不变 (除非str重新赋值)
3.4、title(self)
标题化 以特殊字符或空格隔开,每个字符串首字母大写
1 a = 'abc qwe qaz' 2 print(a.title()) 3 # Abc Qwe Qaz
3.5、center(self, 宽度,填充字符)
*****居中*****
1 str = 'hello' 2 print(str.center(10,*)) 3 #**hello*** *加上hello一共 宽度10 4 5 #如果字符串长9 hello1234*
3.6、startswith() endswith()
startswith(self, prifix, start=none, end=none) 判断是否以**前缀 开头
endswith(self, suffix, start=none, end=none) 判断是否以**后缀 结尾
1 a = 'table' 2 print(a.startswith('t')) 3 print(a.startswith('tab')) 4 print(a.startswith('b',2,5)) #切片之后是个新字符串 'ble' 5 print(a.endswith('le')) 6 # True True True True
3.7、count(self, sub, start=none, end=none)
统计 ** 出现的次数
1 a = 'tableabbleeee' 2 print(a.count('a')) #统计'a'出现的次数 3 print(a.count('bl')) #字符串 整体 与 个体 的关系 4 print(a.count('q')) #没有 就是 0 次 5 print(a.count('e',4,11)) #count() 切片统计'e'出现的次数 6 # 2 7 # 2 8 # 0 9 # 3
3.8、strip(self, chars=None)
lstrip() 做删除
rstrip() 右删除
清除字符串两头的空格
1 a = ' table abbl eeee ' 2 3 print(a.lstrip()) #清除字符串左边的空格 4 print(a.rstrip()) #清除字符串右边的空格 5 print(a.strip()) #清除字符串两头的空格 6 #table abbl eeee 7 # table abbl eeee 8 #table abbl eeee
a = 'tabttle abbl eecba' print(a.strip('abt')) print(a.lstrip('abt')) print(a.rstrip('abt')) #le abbl eec #le abbl eecba #tabttle abbl eec # 'abt' 是可迭代的对象 分解成 'a' ,'b', 't' 然后两头字符 # 串和 'a' ,'b', 't'每个都遍历对比,能找到一样就去掉,碰到找 # 不到的就截止,不在往后遍历对比
3.9、split(self, sep=None, maxsplit=-1)
字符串分割成列表
1 a = 'tabttle abbl eecba' 2 b = 'tabttle,abbl,eecba' 3 print(a.split()) #按空格分割字符串 4 print(b.split(',')) # 按逗号分割字符串 5 print(a.split('t')) #按't'分割字符串 ********t前面、t和t之间,是空字符串,空字符串也是一个字符串 6 print(a.split('t',1)) #按't'分割字符串, 只分割1次,就是2个字符串 7 print(a.split('t',2)) #按't'分割字符串。只分割2次,就是3个字符串
3.10、replace(self, old, new, count=None)
替换
1 a = '苹果 香蕉 黄瓜 苹果 橘子 黄瓜' 2 print(a.replace('苹果','小狗')) #替换所有苹果 3 print(a.replace('苹果','小狗',1)) #替换1次苹果
3.11、format(self, *args, **kwargs) 格式化输出
format(self, 参数,参数)
1 # 大括号里面什么都没有 2 a = 'name:{},age:{},job{},name1:{}'.format('Tom','18','teacher','Tom') 3 print(a) 4 5 # 大括号里面加序号 6 b = 'name:{0},age:{1},job{2},name:{0}'.format('Tom','18','teacher') 7 print(b) #可以省略一部分(Tom) 8 9 #第三种 键值对 10 c= 'name:{name},age:{age},job{job},name:{name}'.format(name = 'Tom',age = '18',job = 'teacher') 11 d= 'name:{name},age:{age},job{job},name:{name}'.format(age = '18',name = 'Tom',job = 'teacher') 12 print(c) #键值对 优点 无视顺序 13 print(d) 14 15 # name:Tom,age:18,jobteacher,name1:Tom 16 # name:Tom,age:18,jobteacher,name:Tom 17 # name:Tom,age:18,jobteacher,name:Tom 18 # name:Tom,age:18,jobteacher,name:Tom
3.12、isalnum() isalpha() isdigit()
isalnum() 判断字符串由 字母或数字 组成
isalpha() 判断字符串由 字母 组成
isdigit() 判断字符串由 数字 组成
1 name = 'Tom123' 2 print(name.isalnum()) 3 print(name.isalpha()) 4 print(name.isdigit()) 5 # True 6 # False 7 # False
四、for 循环
for 变量 in 可迭代对象:
遍历
1 Demo = 'china' 2 for i in Demo: 3 print(i) 4 # c 5 # h 6 # i 7 # n 8 # a