整数(int) ,字符串(str),布尔值(bool),列表(list),元组(tuple),字典(dict),集合(set).
bit_length()就是帮助你快速的计算整数在内存中占用的二进制码的长度.
i=8
print(i.bit_length())
# int ---> str i = 1 s = str(i) # str ---> int s = ‘123‘ i = int (s)
真 1 True。
假 0 False。
# int ---> bool i=3 print(bool(i))
# bool---> int Ture 1 False 0
#str ----> bool s = ""---->False s = "0"--->Ture
s = ‘ABCDEFG‘ # 索引 s1 = s[0] print(s1)#A
s2 =s[-2] print(s2)# F
# 切片:顾头不顾尾 s = ‘ABCDEFG‘ s3 = s[0:3] print(s3) # ABC
s4 = s[0:] print(s4) # ABCDEFG
s5 = s[:] print (s5)# ABCDEFG
s6 = s[0:5:2]# s[首:尾:步长] print(s6)#ACE
s7 = s[4::-1]#倒取 print(s7)#EDCBA
s = ‘afwkDjh‘ s1 = s.capitalize() # 首字母大写 print(s1) #Afwkdjh
# 全大写、全小写 s2 = s.upper() s3 = s.lower() print(s2,s3) # AFWKDJH afwkdjh
# 大小写翻转 s4 = s.swapcase() print(s4) # AFWKdJH
#每隔开(特殊字符或数字)的首字母大写 s = ‘sdf dff*kgj-jia‘ s0 = s.title() print(s0) # Sdf Dff*Kgj-Jia
#居中,空白填充 s = ‘fiheiFIjifa‘ s5 = s.center(30,‘*‘) print(s5) # *********fiheiFIjifa**********
# 字符长度 s = ‘sdidh大哥‘ l = len(s) print(l) #7
# 判断是否是开头 s = ‘alsfiffhis‘ s7 = s.startswith(‘a‘) s8 = s.startswith(‘alsf‘) s81 = s.startswith(‘s‘,2,5) print(s7,s8,s81) # True True True
# 默认去前后空格、也可以指定 s = ‘ & df#siffk%‘ s1 = s.strip() print(s1) #& df#siffk% s2 = s.strip(‘%‘) # & df#siffk s3 = s.strip(‘ &%‘) print(s2,s3) #df#siffk username = input(‘请输入名字:‘).strip() if username == ‘wws‘: print(‘输入正确‘)
# lstrip()删左 rstrip()删右 s = ‘%asua%‘ s1 = s.lstrip(‘%‘) #asua% s2 = s.rstrip(‘%‘) #%asua print(s1,s2)
#计数 s = ‘aasfjajfaidaa‘ s3 = s.count(‘a‘) #6 s4 = s.count(‘a‘,2,) #4 print(s3,s4)
#分割文本 s = ‘ajai*hf ahf*a ifhi*shf idf‘ s5 = s.split() #[‘ajai*hf‘, ‘ahf*a‘, ‘ifhi*shf‘, ‘idf‘] s6 = s.split(‘*‘)#[‘ajai‘, ‘hf ahf‘, ‘a ifhi‘, ‘shf idf‘] print(s5,s6)
#format的三种玩法 格式化输出 name = "wws" age = "18" hobby = ‘ym‘ s =‘我叫{},今年{},爱好{},我叫{}‘.format("wws","18",‘ym‘,"wws") s0 = ‘我叫{0},今年{1},爱好{2},我叫{0}‘.format(name,age,hobby) s1 = ‘我叫{name},今年{age},爱好{hobby},我叫{name}‘.format(age = "18",name = "wws",hobby = ‘ym‘)# print(s,s0,s1)#我叫wws,今年18,爱好ym,我叫wws
# 替换,可指定特换多少个 s = ‘我是一名学生,学生是我‘ s0 = s.replace(‘学生‘,‘老师‘)#我是一名老师,老师是我 s1 = s.replace(‘学生‘,‘老师‘,1)#我是一名老师,学生是我 print(s0,s1)
s_str = ‘ASdf‘ you_str = input("请输入验证码不区分大小写:") if s_str.upper() == you_str.upper(): print(‘验证成功‘) else: print(‘验证错误‘)
原文:https://www.cnblogs.com/2584808136-qq-com/p/12741623.html