一、while循环的补充
while True: name=input(‘please input your name: ‘) password=input(‘please input your password: ‘) if name == ‘egon‘ and password == ‘123‘: print(‘login successfull‘) while True: cmd=input(‘>>: ‘) if cmd == ‘quit‘: break print(‘====>‘,cmd) break
tag=True while tag: name=input(‘please input your name: ‘) password=input(‘please input your password: ‘) if name == ‘egon‘ and password == ‘123‘: print(‘login successfull‘) while tag: cmd=input(‘>>: ‘) # if cmd == ‘quit‘: # tag=False # continue # print(‘====>‘,cmd) if cmd == ‘quit‘: tag=False else: print(‘====>‘,cmd)
while: else:的用法的
count=0 while count < 10: if count == 3: count+=1 continue print(count) count+=1 else: #最后执行 print(‘在最后执行,并且只有在while循环没有被break打断的情况下才执行‘)
二、字符串的内置方法
name=‘egon‘ #name=str(‘egon‘)print(type(name)) 优先掌握
移除空白strip
msg=‘ hello ‘ print(msg) print(msg.strip()) msg=‘***hello*********‘ msg=msg.strip(‘*‘) print(msg) print(msg.lstrip(‘*‘)) print(msg.rstrip(‘*‘))
注:strip只能去除最左边以及最右边的空格,去除不了中间的空格
如:L= a a #三个空格夹着2个a
print(L.strip())
输出的结果是a a
用处:我们不能控制用户的输入,为了判断方便,将无用部分剔除
例:剔除用户误输入的空格,以免影响判断
while True: name=input(‘user: ‘).strip() password=input(‘password: ‘).strip() if name == ‘egon‘ and password == ‘123‘: print(‘login successfull‘)
切分split
字符串切分后变为列表,列表不能再进行切分
info=‘root:x:0:0::/root:/bin/bash‘ print(info[0]+info[1]+info[2]+info[3]) user_l=info.split(‘:‘) print(user_l[0]) msg=‘hello world egon say hahah‘ print(msg.split()) #默认以空格作为分隔符 cmd=‘download|xhp.mov|3000‘ cmd_l=cmd.split(‘|‘) print(cmd_l[1]) print(cmd_l[0]) print(cmd.split(‘|‘,1))#后面的数字代表,切分次数,默认从左往右切分
用处:将字符串按照要求切分成需要的几部分
while True: cmd=input(‘>>: ‘).strip() if len(cmd) == 0:continue cmd_l=cmd.split() print(‘命令是:%s 命令的参数是:%s‘ %(cmd_l[0],cmd_l[1]))
切片:切出子字符串
msg=‘hello world‘print(msg[1:3]) #1 2print(msg[1:4]) #1 2 3
长度len
作用:算出字符串的长度(包括空格)print(len(‘hell 123‘))
索引
作用:找出字符串中某个字符在字符串中的位置name=‘hell 123‘print(name.index(‘h‘)) 掌握部分
oldboy_age=84 while True: age=input(‘>>: ‘).strip() if len(age) == 0:continue if age.isdigit(): age=int(age) else: print(‘must be int‘) #startswith,endswith name=‘alex_SB‘ print(name.endswith(‘SB‘)) print(name.startswith(‘alex‘)) #replace name=‘alex say :i have one tesla,my name is alex‘ print(name.replace(‘alex‘,‘SB‘,1)) #format print(‘my name is %s my age is %s my sex is %s‘ %(‘egon‘,18,‘male‘)) print(‘my name is {} my age is {} my sex is {}‘.format(‘egon‘,18,‘male‘)) print(‘my name is {0} my age is {1} my sex is {0}:{2}‘.format(‘egon‘,18,‘male‘)) print(‘my name is {name} my age is {age} my sex is {sex}‘.format( sex=‘male‘, age=18, name=‘egon‘)) name=‘goee say hello‘ # print(name.find(‘S‘,1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引 # print(name.index(‘S‘)) #同上,但是找不到会报错 print(name.count(‘S‘,1,5)) #顾头不顾尾,如果不指定范围则查找所有 #join info=‘root:x:0:0::/root:/bin/bash‘ print(info.split(‘:‘)) l=[‘root‘, ‘x‘, ‘0‘, ‘0‘, ‘‘, ‘/root‘, ‘/bin/bash‘] print(‘:‘.join(l)) #lower,upper name=‘eGon‘ print(name.lower()) print(name.upper())
了解部分
#expandtabs name=‘egon\thello‘ print(name) print(name.expandtabs(1)) #center,ljust,rjust,zfill name=‘egon‘ # print(name.center(30,‘-‘)) print(name.ljust(30,‘*‘)) print(name.rjust(30,‘*‘)) print(name.zfill(50)) #用0填充 #captalize,swapcase,title name=‘eGon‘ print(name.capitalize()) #首字母大写,其余部分小写 print(name.swapcase()) #大小写翻转 msg=‘egon say hi‘ print(msg.title()) #每个单词的首字母大写 #在python3中 num0=‘4‘ num1=b‘4‘ #bytes num2=u‘4‘ #unicode,python3中无需加u就是unicode num3=‘四‘ #中文数字 num4=‘Ⅳ‘ #罗马数字 #isdigt:str,bytes,unicode print(num0.isdigit()) print(num1.isdigit()) print(num2.isdigit()) print(num3.isdigit()) print(num4.isdigit()) #isdecimal:str,unicode num0=‘4‘ num1=b‘4‘ #bytes num2=u‘4‘ #unicode,python3中无需加u就是unicode num3=‘四‘ #中文数字 num4=‘Ⅳ‘ #罗马数字 print(num0.isdecimal()) # print(num1.) print(num2.isdecimal()) print(num3.isdecimal()) print(num4.isdecimal()) #isnumeric:str,unicode,中文,罗马 num0=‘4‘ num1=b‘4‘ #bytes num2=u‘4‘ #unicode,python3中无需加u就是unicode num3=‘四‘ #中文数字 num4=‘Ⅳ‘ #罗马数字 print(num0.isnumeric()) # print(num1) print(num2.isnumeric()) print(num3.isnumeric()) print(num4.isnumeric()) #is其他 name=‘egon123‘ print(name.isalnum()) #字符串由字母和数字组成 name=‘asdfasdfa sdf‘ print(name.isalpha()) #字符串只由字母组成 # name=‘asdfor123‘ print(name.isidentifier()) name=‘egGon‘ print(name.islower()) print(name.isupper()) print(name.isspace()) name=‘Egon say‘ print(name.istitle())
三、数字的内置方法
=====>part1:数字类型掌握:int,float了解:Long(在python2中才有),complex num=10num=int(10)print(type(num),num) salary=12.5salary=float(12.5)print(type(salary),salary) 进制转换(了解部分)二进制:0 11010101res=1*(2**6)+1*(2**4)+1*(2**2)+1*1print(res) bin(11) #十进制的11转成二进制 八进制:0-7print(oct(11))#十进制的11转成八进制 十六进制:0-9 a-fprint(hex(11))print(hex(16)) 复数x=1-2jprint(x.real)print(x.imag)
时间: 2024-10-14 00:47:45