python自编循环语句_Python循环语句-for/while

#优先掌握的操作:(*****)

1、按索引取值(正向取+反向取) :只能取#msg='hello world'#print(type(msg[0])) #取出的字符还是为str类型#print(msg[-1])#msg[0]='H' #此处要执行修改操作的化会发生报错

​2、切片(顾头不顾尾,步长)#msg='hello world'#print(msg[0]+msg[1]+msg[2])#print(msg[0:5])#print(msg[0:5:2]) #其中0为起始位置 5为结束位置(顾头不顾尾)取不到索引为5的字符 2为步长#print(msg[0:]) #不写默认到最后一个字符#print(msg[:]) #不写起始和结束位置元素默认从第一个切片到最后一个字符

​#print(msg[-1:-5:-1]) #-1 -2 -3 -4#print(msg[::-1]) #-1 -2 -3 -4

​3、长度len:统计的是字符串中字符的个数#msg='h你d'#print(len(msg))

​4、成员运算in和not in:判断一个子字符串是否存在与一个大字符串中#msg='hello world'#print('ho' in msg)#print('ho' not in msg)

​5、移除空白strip:移除字符串左右两边的某些字符。strip后面跟一个参数(即要移除的字符,可以为多个)#msg=' hello '

​#print(msg.strip(' '))#print(msg.strip()) 不写参数,默认参数为空格#print(msg)

​#name=input('name>>>: ').strip() #name='egon'#pwd=input('password>>>: ').strip()#

#if name == 'egon' and pwd == '123':#print('login successfull')#else:#print('username or password error')

​#msg='***h**ello**********'#print(msg.strip('*'))

​#msg='*-=+h/ello*(_+__'#print(msg.strip('*-=+/(_'))

​6、切分split: 把有规律的字符串切成列表从而方便取值,split(分隔符,切几次 不写默认切完)#info='egon:18:180:150'#res=info.split(':',1)#print(res)#print(res[1])

​#info='egon:18:180:150'#res=info.split(':')#print(res)

​#s1=res[0]+':'+res[1]+':'+res[2]+':'+res[3]#s1=''#for item in res:#s1+=item#print(s1)

​#s1=':'.join(res) 专门用于字符串的拼接 参数为一个需要拼接的内容#print(s1)#':'.join([1,2,3,4,5])

​7、循环#for i in 'hello':#print(i)

需要掌握的操作(****)1、strip,lstrip,rstrip #同strip 一个去除左边元素,一个去除右边元素#msg='*****hello****'#print(msg.strip('*'))#print(msg.lstrip('*'))#print(msg.rstrip('*'))

​2、lower,upper #将英文字符(忽略其他字符)全部变为大写或小写#msg='AaBbCc123123123'#print(msg.lower())#print(msg.upper())

​3、startswith,endswith #判断开头结尾是不是特定字母(参数) 返回布尔值#msg='alex is dsb'#print(msg.startswith('alex'))#print(msg.endswith('sb'))

​4、format的三种玩法#msg='my name is %s my age is %s' %('egon',18)#print(msg)

​#msg='my name is {name} my age is {age}'.format(age=18,name='egon')#print(msg)

​#了解#msg='my name is {} my age is {}'.format(18,'egon')#msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon')#print(msg)

​5、split,rsplit#cmd='get|a.txt|33333'#print(cmd.split('|',1))#print(cmd.rsplit('|',1))

​6、replace #替换掉字符串中的指定内容 替换完生成新字符串 并未改变以前的字符串#msg='kevin is sb kevin kevin'#print(msg.replace('kevin','sb',2))

​7、isdigit #当字符串内为纯数字时结果为True,#res='11111'#print(res.isdigit())#int(res)

​#age_of_bk=18#inp_age=input('your age: ').strip()#if inp_age.isdigit():#inp_age=int(inp_age) #int('asdfasdfadfasdf')#if inp_age > 18:#print('too big')#elif inp_age < 18:#print('too small')#else:#print('you got it')#else:#print('必须输入纯数字')

了解(**)1、find,rfind,index,rindex,count#print('xxxkevin is sb kevin'.find('kevin'))#print('xxxkevin is sb kevin'.index('kevin'))#print('xxxkevin is sb kevin'.rfind('kevin'))#print('xxxkevin is sb kevin'.rindex('kevin'))

​#res='xxxkevin is sb kevin'.find('kevasdfsadfin')#print(res)#res='xxxkevin is sb kevin'.index('kevasdfsadfin')#print('kevin is kevin is kevin is sb'.count('kevin'))

​2、center,ljust,rjust,zfill#print('egon'.center(50,'*')) 居中#print('egon'.ljust(50,'*'))#print('egon'.rjust(50,'*'))#print('egon'.zfill(50)) 用0填充

​3、captalize,swapcase,title#print('my name is kevin'.capitalize()) 首字母大写#print('AaBbCc'.swapcase()) 字符串中英文字符(大写变为小写,小写变为大写)#print('my name is kevin'.title()) 每个单词的首字母大写

​4、is其他#name='egon123'#print(name.isalnum()) #字符串由字母或数字组成#print(name.isalpha()) #字符串只由字母组成#print(name.islower())#print(name.isupper())#name=' '#print(name.isspace())#msg='I Am Egon'#print(msg.istitle())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值