Python中常用的字符串内建函数

来源:互联网 发布:下载qq软件2016 编辑:程序博客网 时间:2024/05/16 13:07
    1、string.capitalize()

        把字符串的第一个字符大写。

[python] view plain copy
  1. >>> myString = "hello world, hello everyone!"  
  2. >>> myString.capitalize()  
  3. 'Hello world, hello everyone!'  


        2、string.center(width)

        返回一个原字符串居中,并使用空格填充至长度width的新字符串。可以改变填充使用的字符。

[python] view plain copy
  1. >>> myString.center(40)  
  2. '      hello world, hello everyone!      '  
  3. >>> myString.center(40'*')  
  4. '******hello world, hello everyone!******'  

 

        3、string.count(str, beg=0, end=len(string))

        返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数。

[python] view plain copy
  1. >>> myString.count('hello')  
  2. 2  
  3. >>> myString.count('hello'010)  
  4. 1  


        4、string.find(str, bg=0, end=len(string))

        find方法可以检测str是否包含在string中,如果bg和end制定范围,则检查是否包含在指定范围内,如果是则返回开始的索引值,否则返回-1。

[python] view plain copy
  1. >>> myString.find('world')  
  2. 6  
  3. >>> myString.find('am')  
  4. -1  


       5、string.rfind(str, beg=0,end=len(string))
       类似于 find()函数,不过是从右边开始查找。返回str左边开始的索引值。

[python] view plain copy
  1. >>> myString.rfind('world')  
  2. 6  
  3. >>> myString.rfind('hello')  
  4. 13        #右边的hello的索引值  


        6、string.index(str, beg=0,end=len(string))         和find()方法一样,但是如果str不在string中会报一个异常。

        7、string.rindex(str, beg=0,end=len(string))        和index()一样,但是是从右开始。

[python] view plain copy
  1. >>> myString.index('world')  
  2. 6  
  3. >>> myString.index('am')  
  4.   
  5. Traceback (most recent call last):  
  6.   File "<pyshell#28>", line 1in <module>  
  7.     myString.index('am')  
  8. ValueError: substring not found  


        8、string.islower()

        如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False。

        9、string.isupper()

        如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False。

[python] view plain copy
  1. >>> myString.islower()  
  2. True  
  3. >>> myString.isupper()  
  4. False  


        10、string.isnumeric()
        如果 string 中只包含数字字符,则返回 True,否则返回 False。

 

 

        11、string.join(seq)

        Merges (concatenates) 以string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串。

[python] view plain copy
  1. >>> myString = "+"  
  2. >>> seq = ['8''9']  
  3. >>> myString.join(seq)  
  4. '8+9'  


        12、string.ljust(width)           返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串。

        13、string.rjust(width)           返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。

[python] view plain copy
  1. >>> myString = "Hello world"  
  2. >>> myString.ljust(30)  
  3. 'Hello world                   '  
  4. >>> myString.rjust(30)  
  5. '                   Hello world'  


        14、string.lower()                   转换 string 中所有大写字符为小写。

        15、string.upper()                  转换 string 中的小写字母为大写。

        16、string.swapcase()           翻转 string 中的大小写。

[python] view plain copy
  1. >>> myString = "Hello world!"  
  2. >>> myString.lower()  
  3. 'hello world!'  
  4. >>> myString.upper()  
  5. 'HELLO WORLD!'  
  6. >>> myString.swapcase()  
  7. 'hELLO WORLD!'  


        17、string.lstrip()                     截掉 string 字符串左边的空格。

        18、string.rstrip()                    删除 string 字符串末尾的空格。

        19、string.strip([obj])             在 string 上执行 lstrip()和 rstrip()

 

 

         20、string.replace(str1, str2, num=string.count(str1))

        把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次。

[python] view plain copy
  1. >>> myString = "This is an apple and that is an apple too."  
  2. >>> myString.replace('apple''orange', string.count('apple'))  
  3. 'This is an orange and that is an orange too.'  
  4. >>> myString.replace('apple''orange'1)  
  5. 'This is an orange and that is an apple too.'    #虽然语法有问题  


        21、string.split(str="", num=string.count(str))

        以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串。

[python] view plain copy
  1. >>> myString.split(' '3)  
  2. ['This''is''an''apple and that is an apple too.']  


        22、string.startswith(obj, beg=0,end=len(string))
        检查字符串是否是以 obj 开头,是则返回 True,否则返回False。如果beg 和 end 指定值,则在指定范围内检查

[python] view plain copy
  1. >>> myString.startswith('This')  
  2. True  
  3. >>> myString.startswith('and')  
  4. False  


        23、string.title()
        返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。

        24、string.istitle()
        如果 string 是标题化的(见 title())则返回 True,否则返回 False。

[python] view plain copy
  1. >>> myString = "This is an apple and that is an apple too."  
  2. >>> myString.istitle()  
  3. False  
  4. >>> str = myString.title()  
  5. >>> str  
  6. 'This Is An Apple And That Is An Apple Too.'  
  7. >>> str.istitle()  
  8. True  


        25、string.zfill(width)

        返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0。

[python] view plain copy
  1. >>> myString = "Hello world!"  
  2. >>> myString.zfill(30)  
  3. '000000000000000000Hello world!'