Python字符串中常见的函数

字符串中常见的函数

  1. capitalize()将字符串第一个字母变为大写
    s = 'abcdef0'
    s.capitalize()
    'Abcdef0'
  2. casefold()将字符串里所有字母小写
    s2 = 'ABCDEF'
    s2.casefold()
    'abcdef'
  3. cente(width)将字符串居中长度为width
    s2 = 'ABCDEF'
    s2.center(30)
    '            ABCDEF            '
  4. count(s)查找s在字符串中出现的次数
    s2 = 'ABCDEF'
    s2.count('A')
    1
  5. endswith(s)检查字符串是否以s结尾
    s2 = 'ABCDEF'
    s2.endswith('F')
    True
  6. expandtabs()把字符串中tab符号(\t)转化为空格
    s3 = 'abc\tdef\tg'
    s3.expandtabs()
    'abc     def     g'
  7. find()查询字符串中是否包含
    s = 'abcde'
    s.find('a')
    0
    s.find('Y')
    -1
  8. join(123)以123为分割将其他字符串插入
    s = 'abc'
    s.join('123')
    '1abc2abc3'
  9. repalace(old,new)将old替换为new
    s = 'abcd f'
    s.replace('f','e')
    'abcd e'
  10. split()默认用按照空格将字符串切片,可添加参数进行切片
    s = 'a b c d'
    s.split()
    ['a', 'b', 'c', 'd']
  11. swapcase()反转大小写
    s = 'AbCd'
    s.swapcase()
    'aBcD'
  12. title()将所有单词首字母大写其余字母小写
    s = 'where are you'
    s.title()
    'Where Are You'

字符串格式化函数format

格式化字符串 format
'{0},{1},c'.format('a','b')
'a,b,c'
'{{0}}'.format('a')
'{0}'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值