字符串方法


    find  join    lower    replace    split    strip    translate


find:

    可以在一个较长的字符串中查找字符串,返回值是这个字符串所在的位置的最左端索引,找不到返回 -1

    例:

     >>>‘With a moo-moo here,and a moo-moo there’.find('moo')

    7

    >>> ‘With a moo-moo here,and a moo-moo there’.find('hello')

    -1


join:

    序列中的元素,是 split 的逆方法

    例:

    >>>sss = ['1','2','3']  #注意:这里必须加引号,因为它只能连接字符串

     >>>ccc = '+'

    >>>ccc.join(sss)

    '1+2+3'


    >>>dirs = '','usr','bin','env'

    >>> '/'.join(dirs)

    '/usr/bin/env'

    >>> print 'C:' + '\\'.join(dirs)

    C:\usr\bin\env


lower:

    返回字符中的小写字母

    例:

    >>>‘Where Are You’.lower()

    'where are you'


replace:

    某字符串的所有匹配项均被替换之后得到的字符串

     例:

    >>>'This is a test'.replace('is','eez')

    'Theez eez a test'


split:

    将字符串分割成序列,join方法的逆方法

    例:

    >>>‘1+2+3+4’.split('+')    #此处若不指定任何分割符,则以空格为分割符(空格,制表,换行等)

    ['1','2','3','4']


strip:

    返回去除两侧空格的字符串(不包括内部)

    例:

     >>>‘     where are you    ’.strip()

    'where are you'

    >>>'***where * are * you *********!'.strip(*!)

    'where * are * you'


translate:

    替换字符串中的某些部分,只处理单个字符