不常用方法

  1. find.rfind,index,rindex,count
    作用:找到起始索引
    msg='hello world'
    msg.find('e') ----->1
    rfind(反方向)
    ps:index与rindex的用法与find类似,但是若找不到时,find返回-1,index会异常

conut
用法:统计
msg='abc qwe abc abc'
msg.count('abc') ----->3

2.center,ljust,rjust,zfill
用法:
'abc'.center(10,'') 将abc居中两侧用补齐至10
'abc'.ljust(10,'') 将abc左对齐右侧用补齐至10
'abc'.rjust(10,'') 将abc右对齐左侧用补齐至10
'abc'.zfill(10,'*') 右对齐用0补齐至10

3.expandtabs
用法:设置制表符代表的空格
msg='hello\tworld'
msg.expandtabs(2) ----->hello world

4.captalize,swapcase,title
msg='hello world'
msg.captalize() ----->Hello world 首字母大写
msg.swapcase() ----->HELLO WORLD 反转大小写
msg.title() ----->Hello World 每个单词首字母大写

  1. islower print('abc'.islower()) True 判断是否全部小写
    isupper print('ABC'.isupper()) True 判断是否全部大写
    istitle print('Hello World'.istitle()) True 判断每个单词首字母是否大写
    isalnum print('123123aadsf'.isalnum()) True 判断是否由字母和数字组成
    isalpha print('ad'.isalpha()) True 判断字符串是否由纯字母组成
    isspace print(' '.isspace()) True 判断是否由空格组成
    isidentifier print('print'.isidentifier()) 用来判断变量名是否可用

猜你喜欢

转载自www.cnblogs.com/bailongcaptain/p/12458203.html