标签:大小写 split() welcome div 查找 删除前后空格 this com obb
i = 2 print(i.bit_length()) 值为2 二进制00000010 i = 5 print(i.bit_length()) 值为3 二进制00000101
i = 3 b = bool(i) print(b)
True 1 False 0 i = int(True) print(i) 值为1
s = "" -----> False 非空字符串都是True s = "0" -----> True
s1 = s[0] print(s1) A s2 = s[2] print(s2) C s3 = s[-1] print(s3) F s4 = s[-2] print(s4) R
顾头不顾尾,尾需要加1
s = ‘ABCDLSESRF‘ s5 = s[0:4] print(s5) #ABCD
s6 = s[0:-1] print(s6) #ABCDLSESR
s7 = s[:] #s7和s8效果一样,取完后边什么都不写 s8 = s[0:] print(s7,s8) #ABCDLSESRF ABCDLSESRF
s9 = s[0:0] #空
步长:
s = ‘ABCDLSESRF‘ # s[首:尾:步长] s10 = s[0:5:2] #隔两个取一次 print(s10) #ACL s11 = s[4:0:-1] print(s11) #LDCB s12 = s[3::-1] print(s12) #DCBA s13 = s[3::-2] print(s13) #DB
s = ‘ABCDLSESRF‘ s13 = s[-5:-1] #表示从-5到-1的截取显示 print(s13) #SESR s14 = s[-1::-1] #表示以-1开头,一直到开始结束,每次以-1的形式显示 print(s14) #FRSESLDCBA s15 = s[::-1] #表示全部截取,以-1的形式显示,每次显示-1个 print(s15) #FRSESLDCBA
# 字符串第一个字母大写
s = ‘arthur‘ s1 = s.capitalize() print(s1)
#Arthur
s = ‘my +na7me,is*authur‘ print(s.title()) #My +Na7Me,Is*Authur
全大写
s = ‘my name is Authur 7 周日‘ print(s.upper())
#MY NAME IS AUTHUR 7 周日 #字母和中文是不受影响的
s = ‘MY NAME IS AUTHUR 7 周日‘ print(s.lower()) #my name is authur 7 周日
输入验证码时,不分大小写
s_str= ‘acEQ1‘ you_input =input(‘请输入验证码,不区分大小写: ‘) if s_str.upper() == you_input.upper(): print(‘输入成功‘) else: print(‘请重新输入‘)
大小写翻转
s = ‘mY nAme iS aUTHUR‘ print(s.swapcase())
#My NaME Is Authur
s = ‘Authur‘
print(s.center(20,‘+‘)) #~号是填充位,不填默认是空格,20位左右表示左右加起来填充20位
#+++++++Authur+++++++
s = ‘authur\tsir‘
print(s.expandtabs()) #结果为authur sir #所以意味着\t补了三位
s = ‘authuris\tsir‘
print(s.expandtabs()) #结果为authuris sir #所以意味着\t补了八位,因为前面的字符串已经满足八位
s = ‘authur‘ print(len(s)) #6
s = ‘authur‘ print(s.startswith(‘aut‘,0,5)) #默认开始和结尾不写是判断全部
#True
s = ‘Authur‘ print(s.endswith(‘t‘,0,3)) #默认什么都不写是判断全部,判断选定范围内的尾部是否为指定字符串结尾
#True
s = ‘authur‘ print(s.find(‘t‘,0,4))
#2
s = ‘* 7authur # ‘ print(s.strip(‘* 7#‘))
#authur
username = input(‘请输入名字:‘).strip().lower() if username ==‘authur‘: info =‘‘‘ **************************** **welcome {}‘s cnblogs** ****************************‘‘‘.format(‘authur‘) print(info)
s = ‘authur‘ print(s.count(‘u‘,0,3))
#1
s = ‘Arthur:this:my:name‘ l = s.split(‘:‘) print(l)
#[‘Arthur‘, ‘this‘, ‘my‘, ‘name‘]
format的三种玩法 格式化输出 s = ‘我叫{},今年{},爱好{},再说一下我叫{}‘.format(‘authur‘,18,‘python‘,‘authur‘) print(s) name = input(‘请输入名字:‘) s = ‘我叫{0},今年{1},爱好{2},再说一下我叫{0}‘.format(name,18,‘python‘) print(s) name = input(‘请输入名字:‘) s = ‘我叫{name},今年{age},爱好{hobby},再说一下我叫{name}‘.format(age=18,name=name,hobby=‘python‘) print(s)
s = ‘my name is xiyangyang and xiyangyang‘ print(s.replace(‘xiyangyang‘,‘authur‘,1))
#my name is authur and xiyangyang
#字符串由字母或数字组成 print(name.isalnum()) #字符串只由字母组成 print(name.isalpha()) #字符串只由数字组成 print(name.isdigit())
标签:大小写 split() welcome div 查找 删除前后空格 this com obb
原文地址:https://www.cnblogs.com/Arthur7/p/9310814.html