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)

  • 相关阅读:
    yii 引入文件
    CodeForces 621C Wet Shark and Flowers
    面试题题解
    POJ 2251 Dungeon Master
    HDU 5935 Car(模拟)
    HDU 5938 Four Operations(暴力枚举)
    CodeForces 722C Destroying Array(并查集)
    HDU 5547 Sudoku(dfs)
    HDU 5583 Kingdom of Black and White(模拟)
    HDU 5512 Pagodas(等差数列)
  • 原文地址:https://www.cnblogs.com/yan-xiang/p/6595284.html
Copyright © 2011-2022 走看看