• str的常用用法简单整理(其他读源码) Carol


    #字符串的索引和切片
    '''
    s = 'AbcdeF' #s[首:尾:步长]
    '''
    # s1 = s[-2]
    # print(s1)
    #切片 :顾头不顾尾。
    # s1 = s[0:4]
    # print(s1)
    # s2 = s[0:-1]
    # print(s2)
    # s3 = s[:]
    # print(s3)
    # s4 = s[0:]
    # print(s4)
    # s5 = s[:-1]
    # print(s5)
    # s6 = s[0:0]
    # print(s6)
    # s7 = s[0:5]
    # print(s7)
    # s8 = s[0:4:2]
    # print(s8)
    # s9 = s[5:2:-1] #逆向输出
    # print(s9)
    # s1 = s[3::-1]
    # print(s1)
    # s2 = s[3::-2]
    # print(s2)
    # s3 = s[-1::-1]
    # print(s3)
    # s4 = s[::-1]
    # print(s4)
    # if s3 == s4:
    # print(0)
    '''
    s = 'aleX'
    #首字母大写
    s1 = s.capitalize()
    print(s1)
    ''' #1,首字母大写
    '''
    s2 = s.upper() #全大写
    sq = s.lower() #全小写
    print(s2,sq)
    ''' #2,全大写,全小写
    '''
    s3 = s.swapcase()
    print(s3)
    ''' #3,大小写反转
    '''
    s = 'aq-ex*e 3 go2n-wusir'
    s4 = s.title()
    print(s4)
    ''' #4,每个隔开(特殊字符或者数字)的单词首字母大写。
    '''
    s = 'alexWUsir'
    s5 = s.center(21,'。')
    print(s5)
    ''' #5,居中,空白填充。
    '''
    s = 'alexaaaa\tsir'
    s6 = s.expandtabs()
    print(s6)
    ''' #6,自动填八位,满八位,填16位。

    '''
    s = 'alex二哥'
    #公共方法
    l = len(s)
    print(l)
    ''' #7,字符串长度。
    '''
    s = 'alexWUsir'
    s7 =s.startswith('alex')
    s71 = s.startswith('x',3,5)
    print(s7,s71)
    ''' #8,以什么开头
    '''
    find 通过元素找索引,找不到返回-1

    index通过元素找索引,找不到报错


    s = 'alexWUsir'
    s8 = s.find('A')
    # s81 = s.index('A') 直接报错
    print(type(s8))
    ''' #9,查找索引
    '''
    s = 'alexWUsir%'
    s9 = s.strip('%')
    print(s9)
    s = ' *a%lexWUsi* r%'
    s91 = s.lstrip(' %*')
    print(s91)
    # username = input('请输入名字:').strip()
    # if username =='春哥':
    # print('恭喜春哥发财')
    ''' #10,strip 默认删除前后空格,lstrip 左
    '''
    s = 'alexaa wusirl'
    s10 = s.count('e')
    print(s10)
    ''' #11,count 查看某字符串s10在s里面的个数。
    # split str ---->list


    '''
    s = ';alex;wusir;taibai'
    l = s.split('a')
    print(l)
    ''' #12,分割,str转list



    '''
    s = '我叫{},今年{},爱好{},再说一下我叫{}'.format('太白',36,'girl','太白')
    print(s)
    name = input('请输入名字:')
    s = '我叫{0},今年{1},爱好{2},再说一下我叫{0}'.format(name,36,'girl')
    print(s)
    name = input('请输入名字:')
    s = '我叫{name},今年{age},爱好{hobby},再说一下我叫{name}'.format(age=18,name=name,hobby='girl')
    print(s)
    ''' #13,format的三种玩法 格式化输出
    '''
    s = '你好漂亮你好我们再见'
    s11 = s.replace('你好','再见',2)
    print(s11)
    ''' #14,replace 替换原来str中的某个元素,可以加次数。
  • 相关阅读:
    C# 中自定义配置
    git 打标签
    状态模式
    组合模式
    intellij自动生成java代码注释(java文件注释和方法注释)
    git版本回退
    Error:Unable to make the module:***, related gradle configuration was not found. Please, re-import the Gradle project and try again.
    Typo: In word 拼写检查
    javax.persistence.EntityNotFoundException: Unable to find报错
    报错org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
  • 原文地址:https://www.cnblogs.com/qinghuani/p/8059276.html
Copyright © 2020-2023  润新知