1. 程式人生 > >Python中常用的字串內建函式

Python中常用的字串內建函式

        1、string.capitalize()

        把字串的第一個字元大寫。

>>> myString = "hello world, hello everyone!"
>>> myString.capitalize()
'Hello world, hello everyone!'


        2、string.center(width)

        返回一個原字串居中,並使用空格填充至長度width的新字串。可以改變填充使用的字元。

>>> myString.center(40)
'      hello world, hello everyone!      '
>>> myString.center(40, '*')
'******hello world, hello everyone!******'

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

        返回 str 在 string 裡面出現的次數,如果 beg 或者 end 指定則返回指定範圍內 str 出現的次數。

>>> myString.count('hello')
2
>>> myString.count('hello', 0, 10)
1


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

        find方法可以檢測str是否包含在string中,如果bg和end制定範圍,則檢查是否包含在指定範圍內,如果是則返回開始的索引值,否則返回-1。

>>> myString.find('world')
6
>>> myString.find('am')
-1


       5、string.rfind(str, beg=0,end=len(string))
       類似於 find()函式,不過是從右邊開始查詢。返回str左邊開始的索引值。

>>> myString.rfind('world')
6
>>> myString.rfind('hello')
13        #右邊的hello的索引值


        6、string.index(str, beg=0,end=len(string))         

和find()方法一樣,但是如果str不在string中會報一個異常。

        7、string.rindex(str, beg=0,end=len(string))        和index()一樣,但是是從右開始。

>>> myString.index('world')
6
>>> myString.index('am')

Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    myString.index('am')
ValueError: substring not found


        8、string.islower()

        如果 string 中包含至少一個區分大小寫的字元,並且所有這些(區分大小寫的)字元都是小寫,則返回 True,否則返回 False。

        9、string.isupper()

        如果 string 中包含至少一個區分大小寫的字元,並且所有這些(區分大小寫的)字元都是大寫,則返回 True,否則返回 False。

>>> myString.islower()
True
>>> myString.isupper()
False


        10、string.isnumeric()
        如果 string 中只包含數字字元,則返回 True,否則返回 False。

        11、string.join(seq)

        Merges (concatenates) 以string 作為分隔符,將 seq 中所有的元素(的字串表示)合併為一個新的字串。

>>> myString = "+"
>>> seq = ['8', '9']
>>> myString.join(seq)
'8+9'


        12、string.ljust(width)           返回一個原字串左對齊,並使用空格填充至長度 width 的新字串。

        13、string.rjust(width)           返回一個原字串右對齊,並使用空格填充至長度 width 的新字串。

>>> myString = "Hello world"
>>> myString.ljust(30)
'Hello world                   '
>>> myString.rjust(30)
'                   Hello world'


        14、string.lower()                   轉換 string 中所有大寫字元為小寫。

        15、string.upper()                  轉換 string 中的小寫字母為大寫。

        16、string.swapcase()           翻轉 string 中的大小寫。

>>> myString = "Hello world!"
>>> myString.lower()
'hello world!'
>>> myString.upper()
'HELLO WORLD!'
>>> myString.swapcase()
'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 次。

>>> myString = "This is an apple and that is an apple too."
>>> myString.replace('apple', 'orange', string.count('apple'))
'This is an orange and that is an orange too.'
>>> myString.replace('apple', 'orange', 1)
'This is an orange and that is an apple too.'    #雖然語法有問題


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

        以 str 為分隔符切片 string,如果 num有指定值,則僅分隔 num 個子字串。

>>> myString.split(' ', 3)
['This', 'is', 'an', 'apple and that is an apple too.']


        22、string.startswith(obj, beg=0,end=len(string))
        檢查字串是否是以 obj 開頭,是則返回 True,否則返回False。如果beg 和 end 指定值,則在指定範圍內檢查

>>> myString.startswith('This')
True
>>> myString.startswith('and')
False


        23、string.title()
        返回"標題化"的 string,就是說所有單詞都是以大寫開始,其餘字母均為小寫(見 istitle())。

        24、string.istitle()
        如果 string 是標題化的(見 title())則返回 True,否則返回 False。

>>> myString = "This is an apple and that is an apple too."
>>> myString.istitle()
False
>>> str = myString.title()
>>> str
'This Is An Apple And That Is An Apple Too.'
>>> str.istitle()
True


        25、string.zfill(width)

        返回長度為 width 的字串,原字串 string 右對齊,前面填充0。

>>> myString = "Hello world!"
>>> myString.zfill(30)
'000000000000000000Hello world!'