zoukankan      html  css  js  c++  java
  • Python-字符串常用操作方法

    count()

    '''方法作用:在给定的字符串中,查找子串的出现次数且可指定一个区间,在字符串的该区间内进行查找
     方法参数:子串,

           [开始区间]

           [结束区间](不包含)
     方法返回值:int
    '''
    str='abcd@abcd'
    print str.count('a',0,5)

    decode()

    '''方法作用:以encoding指定的编码格式对字符串进行解码,默认的编码为字符串编码
     方法参数:encoding:要使用的编码,如"UTF-8"
            [errors]:设置不同错误的处理方案,一般引起UnicodeError
       方法返回值:返回解码后的字符串
    '''

    str = "hello world!!!"
    str = str.encode('base64','strict')

    print str
    print str.decode('base64','strict')

    encode()

    '''方法作用:以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案
     方法参数:encoding:要使用的编码
          [errors]:设置不同错误的处理方案,一般引起UnicodeError
     方法返回值:返回编码后的字符串
    '''
    str = "你好!";
    print str.encode('base64','strict')

    endswith()

    '''方法作用:判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False,且可指定字符串区间内进行判断
      方法参数:sub
             [开始区间]
                    [结束区间](不包含)
      方法返回值:True,False
    '''
    str = "hello world!!!"
    print dir(str)
    print str.endswith('!')
    print str.endswith('o',0,5)

    find()

    '''方法作用:检测字符串中是否包含子字符串 str
       方法参数:sub
                     [开始索引]
                     [结束索引](不包含)
       方法返回值:有该子串,返回其索引
                        没有该子串,返回-1
    '''
    str = "hello world!!!"
    print dir(str)
    print str.find('o')
    #不包含索引4
    print str.find('o',0,4)

    join()

    '''方法作用:将序列中的元素以指定的字符连接生成一个新的字符串
       方法参数:序列
       方法返回值:返回通过指定字符连接序列中元素后生成的新字符串
    '''
    ljf='+'
    str='123'
    l=['a','b']
    print ljf.join(str)
    print ljf.join(l)

  • 相关阅读:
    1025 反转链表
    Vue--修饰符
    Vue--watch
    Vue--防止页面闪烁
    Vue--过滤器
    Vue--自定义指令
    Vue--生命周期
    vue--父子组件传递数据
    vue--父子组件调用彼此的方法
    Celery--beat
  • 原文地址:https://www.cnblogs.com/yan-xiang/p/6595284.html
Copyright © 2011-2022 走看看