python基础03
python基础03
什么数据类型?
int :用于计算
#int ----->str i = 1 s = str(i) #str----->int(str只能是数字) s = '123' i = int(s) #int ---->bool 只要是0---》False 非0就是True i = 3 b = bool(i) print(b) #bool---->int #True 1 #False 0 ''' ps : while True: pass while 1:#效率高 pass #因为,True要转换成1,所以,要慢些。 '''
eg:1,2,3
bool:用于判断 True,False
#bool True False
str:储存少量数据,进行操作
#str---->bool #s = ""------>False #非空字符串都是True #s = "0"------>True #s = ""-------->False #非空字符串都是True #s = "0"------->True #字符串的索引与切片 s = 'ADCDLSESRF' #索引 s1 = s[0] print(s1) #ABCD 切片:顾头不顾尾 s3 = s[0:4] print(s3) s3 = s[-1] print(s3)
s6 = s[0:-1] print(s6) s7 = s[:] s8 = s[0:] print(s7,s8)
s9 = s[0:0] print(s9)
s ='ABCDLSESRF' #s[首:尾:步长] #s10 = s[0:5:2] #print(s10)
s11 = s[4:0:-1] print(s11) s12 = s[3::-2] print(s12)
s = 'ABCDLSESRF' s13 = s[-1::-1] print(s13)
#字符串的操作 s = 'aleswusir' s1 = s.capitalize()#首字母大写 print(s1) #全大写,全小写 s2 = s.upper() s21 = s.lower() print(s1,s2) #eg: s_str = 'acEQ1' you_input = input('请输入验证码,不区分大小写') if s_str.upper() == you_input.upper(): print('输入成功') else: print('请重新输入')
#大小写翻转 s3 = s.swapcase() print(s3)
#每个隔开(特殊字符或者数字)的单词首字母大写 s = 'alex*egon-wusir' s4 = s.title() print(s4)
#居中,空白填充 s = 'alexWUsir' s5 = s.center(20,'~') print(s5)
#以什么开头结尾endswith #s = 'alexWUsir' #s7 = s.startswith('alex') #s71 = s.startswith('e',2,5) #print(s71) s = 'alexWUsir' s7 = s.startwith('alex') ''' if s7: pass elif s.startswith('b1'): pass print(s7) '''
s = 'alex二哥' #公共方法 l = len(s) print(l)
#find 通过元素找索引,找不到返回-1 s= 'alexWUsir' s8 = s.find('W') print(s8)
print(s8,type(s8))
#index通过元素找索引,找不到报错 s = 'alexWUsir' s8 = s.find('A') s81 = s.index('A') print(s81,type(s8))
#strip默认删除前后空格 s = ' alexWUsir ' s9 = s.strip() print(s9) #eg: username = input('请输入名字:').strip() if username == '春哥': print('恭喜春哥发财')
s = 'alexWUsir%' s9 = s.strip('%') print(s9)
s = ' *a%lexWUsir%' s91 = s.strip(' %*') print(s91) s = 'alexaa wusirl' s10 = s.count('a') print(s10)
s = ' alexaa wusirl' s10 = x.count('y') print(s10)
#split s = ' :alex:wusir:taibai' l = s.split(':') print(l)
#split str---->list s = ' :alex:wusir:taibai' l = s.split('a') print(l)
#format的三种玩法,格式化输出 s = '我叫{},今年{},爱好{},再说一下我叫{}'.format('太白',36,'girl','太白') print(s) s = '我叫{0},今年{1},爱好{2},再说一下我叫{0}'.format('太白',36,'girl') print(s)
s = '我叫{name},今年{age},爱好{hobby},再说一下我叫{name}'.format(age=18,name='太白',hobby='girl') print(s)
name = input('请输入名字:') s = '我叫{name},今年{age},爱好{hobby},再说一下我叫{name}'.format(age=18,name=name,hobby='girl') s = '来看待街坊邻居复合大师街坊法好的撒见客户' s11 = s.replace('街坊','老王') print(s11)
s = 'fhdsklfds' for i in s: print(i)
eg:'fjdsal','二哥',‘13234’
list:储存大量的数据
eg:[1,2,3,'泰哥',[1,2,3]]
元祖:只读。
eg:(1,2,3,'第三方')
dict:字典{'name':'云姐',age:16}
集合:{1,2,34,'asdf'}
逻辑语句的True,False.
print(5 > 4 or 3)#3
print(2>1 or 6)#True