002python路--字符
字符
if age.isdigit(): #字符串age是数字”12”
age = int(age)
else:
exit() #退出程序
exit(“现在我退出了”)
st.isalpha()#是否全是字符
st.isalnum()#是否数字+字母组成
st.isspace()#就一空格,返回True
长度 len(st)
逆序 st[::-1]
截取 print('helloworld'[2:])
属于 print('el' in 'hello')
方法
print( st.count(内容 , start = 0 , end = len(st) )) ) # 统计元素个数
print( st.endswith('tty3') ) # 判断是否以某个内容结尾
print( st.startswith('he') ) # 判断是否以某个内容开头
print(st.find('t' , start = 0 , end = len(st) ) ) # 返回索引值, -1未找到
print('My title title'.rfind('t'))
print('\tMy tLtle\n'.strip()) #去换行符,空格
print('\tMy tLtle\n'.lstrip())
print('\tMy tLtle\n'.rstrip())
print('My title title'.replace('itle','lesson',1)) #把itle替换为lesson ,替换几次
print('My title title'.split('i',1)) #仅以第一个i为分隔符 , 返回列表
print('Abc'.islower())
print('ABC'.isupper())
print(' e'.isspace())
print(' tLtle'.lower())
print('tLtle'.upper())
print( st.capitalize() ) 首字母大写
print('My tLtle'.swapcase()) 大写变小写,小写变大写
print('My tLtle'.title()) 每个单词首字母大写
print("".casefold()) 大写全部变小写
print( st.center(50,'#') ) 居中 长度50,#填充
print('My tLtle'.ljust(50,'*')) 左对齐,*占位
print('My tLtle'.rjust(50,'*')) 右
print("".encode()) 将字符串编码成bytes格式
print(st.partition("hello") )#以hello为分隔符,分成三段
print(st.splitlines() ) #hello\nworld,->['hello' , 'world']

浙公网安备 33010602011771号