python 字符串方法

重要的方法:count、startwith、center、find、lower、upper、strip


print(st.count("o")) #o的个数
print(st.capitalize())  #首字母大写
print(st.center(30,"-"))  #居中
print(st.endswith("pp")) #判断以什么结尾  返回boolean型
print(st.startswith("pp")) #判断以什么开头  返回boolean型
print(st.expandtabs(10))   #/t的个数
print(st.find("z"))  #查找到第一个元素并返回
print(st.format(st1 = "nihao",age = 23))   #格式化输出的另一种方式  建议
print(st.format_map({'st1':'nihao','age' : 22}))   #格式化输出字典型
print(st.index("z"))   #类似于find 区别在于不存在该字符串 index报错  find 返回-1
print(st.isalnum()) #判断字符串是否包含数字或者字母  返回boolean型
print('213242'.isdecimal())  #判断是否为十进制 返回boolean型
print('213242.43'.isdigit())  #判断是否为像数字 整型   返回boolean型  = isnumeric()
print('213242.43'.isnumeric()) #判断是否为像数字 整型   返回boolean型
print(st.isidentifier())       #判断是否为非法变量  变量命名规则 返回boolean型
print(st.islower())            #判断是否为全部小写       返回boolean型
print(st.isupper())            #判断是否为全部大写       返回boolean型
print(' '.isspace())           #判断是否是个空格         返回boolean型
print('My Title'.istitle())            #判断是否为标题,每个单词首字母大写  返回boolean型
print(st.lower())              #大写变小写
print(st.upper())              #小写变大写
print('My Title'.swapcase())   #大写变小写,小写变大写
print('My Title'.ljust(50,'-'))  #靠左
print('My Title'.rjust(50,'-'))  #靠右
print(' my title '.strip())      #将开头或者结尾的空格/换行符 去掉
print(' my title '.lstrip())      #将开头的空格/换行符 去掉
print(' my title '.rstrip())      #将结尾的空格/换行符 去掉
print('hello gc'.replace('hello','hi')) #替换
print('hello gc'.rfind('l')) #从右往左 字符的真实索引位置
print('hello gc nihao'.split(' ')) #根据某个字符分割成列表
print('hello gc nihao'.rsplit('n',1)) #从左往右根据某个字符分割一次
print('hello gc nihao'.title())     #转化为标题,首字母大写
print(' '.join('12'+'45'+'34')) ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值