Python字符串對象常用方法


    安利一句話:字符串是不可變的對象,所以任何操作對原字符串是不改變的!

1.字符串的切割
    def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
        """
        S.split(sep=None, maxsplit=-1) -> list of strings
        
        Return a list of the words in S, using sep as the
        delimiter string.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified or is None, any
        whitespace string is a separator and empty strings are
        removed from the result.
        """
        return []

        用法:返回字符串中所有單詞的列表,使用 sep 作為分隔符(默認值是空字符(空格))用什么切就去掉什么。
             可以使用 maxsplit 指定最大切分數。
        例子:     s = 'STriSSB'
                  print(s.split('STriSSB')) ----> ['', '']
                  print(s.split('S')) ----> ['', 'Tri', '', 'B']
        注意:如果切分的參數 sep 在字符串的左邊或者右邊,最后切得的鏈表中會存在一個空字符串。(如:['',等])
             如果切分的參數 sep 是整個字符串,那么切分結果為兩個空字符串組成的列表。(['',''])

    def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
        """
        S.rsplit(sep=None, maxsplit=-1) -> list of strings
        
        Return a list of the words in S, using sep as the
        delimiter string, starting at the end of the string and
        working to the front.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified, any whitespace string
        is a separator.
        """
        return []

        用法:返回字符串中所有單詞的列表,使用 sep 作為分隔符(默認值是空字符(空格))
             可以使用 maxsplit 指定最大切分數。只不過切的時候是從右邊開始切。
        例子:    s = 'STriSSB'
                print(s.split('STriSSB'))       ----------> ['', '']
                print(s.rsplit('STriSSB'))      ----------> ['', '']
                print(s.split('S'))             ----------> ['', 'Tri', '', 'B']
                print(s.rsplit('S'))            ----------> ['', 'Tri', '', 'B']
                print(s.split('S', maxsplit=1)) ----------> ['', 'TriSSB']
                print(s.rsplit('S', maxsplit=1))----------> ['STriS', 'B']

2.字符串連接
    def join(self, iterable): # real signature unknown; restored from __doc__
        """
        S.join(iterable) -> str
        
        Return a string which is the concatenation of the strings in the
        iterable.  The separator between elements is S.
        """
        return ""

        用法:用S連接序列iterable的所有元素並返回。序列iterable的元素必須全是字符串。
             join()方法是split()方法的逆方法,用來把列表中的個字符串聯起來。

3.字符串的查找
    def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.find(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0

    用法:返回字符串sub的第一個索引,如果不存在這樣的索引則返回-1,可定義搜索的范文為S[start:end].

    def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.index(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found, 
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Raises ValueError when the substring is not found.
        """
        return 0

        用法:返回字符串sub的第一個索引,或者在找不到索引的時候引發 ValueError異常,可定義索引的范圍為S[start:end].

    def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.startswith(prefix[, start[, end]]) -> bool
        
        Return True if S starts with the specified prefix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        prefix can also be a tuple of strings to try.
        """
        return False

        用法:檢測S是否是以prefix開始,可定義搜索范圍為S[start:end].

    def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.endswith(suffix[, start[, end]]) -> bool
        
        Return True if S ends with the specified suffix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        suffix can also be a tuple of strings to try.
        """
        return False

        用法:檢測S是否以suffix結尾,可定義搜索范圍為S[start:end]


4.字符串的替換
    def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
        """
        S.replace(old, new[, count]) -> str
        
        Return a copy of S with all occurrences of substring
        old replaced by new.  If the optional argument count is
        given, only the first count occurrences are replaced.
        """
        return ""

        用法:返回字符串的副本,其中old的匹配項都被替換為new,可選擇最多替換count個(從左往右替換),默認替換全部。


5.字符串的刪除
    def strip(self, chars=None): # real signature unknown; restored from __doc__
        """
        S.strip([chars]) -> str
        
        Return a copy of the string S with leading and trailing
        whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        """
        return ""

        用法:返回字符串的副本,其中所有的chars(默認為空格)都被從字符串的開頭和結尾刪除(默認為所有的空白字符,如空格,tab和換行符。)

    def lstrip(self, chars=None): # real signature unknown; restored from __doc__
        """
        S.lstrip([chars]) -> str
        
        Return a copy of the string S with leading whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        """
        return ""

        用法:返回一個字符串副本,其中所有的char(默認為所有的空白字符,如空格,tab和換行符。)都被從字符串左端刪除。

    def rstrip(self, chars=None): # real signature unknown; restored from __doc__
        """
        S.rstrip([chars]) -> str
        
        Return a copy of the string S with trailing whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        """
        return ""
        
        用法:返回一個字符串副本,其中所有的char(默認為所有的空白字符,如空格,tab和換行符。)都被從字符串右端刪除。


6.統計字符串出現次數
    def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.count(sub[, start[, end]]) -> int
        
        Return the number of non-overlapping occurrences of substring sub in
        string S[start:end].  Optional arguments start and end are
        interpreted as in slice notation.
        """
        return 0

        用法:計算字符串sub的出現次數,可以定義搜索的范圍為S[start:end]


7.字符串字母大小寫轉換
    def upper(self): # real signature unknown; restored from __doc__
        """
        S.upper() -> str
        
        Return a copy of S converted to uppercase.
        """
        return ""

        用法:返回字符串的副本,其中所有小寫字母都轉換為大寫字母。


    def lower(self): # real signature unknown; restored from __doc__
        """
        S.lower() -> str
        
        Return a copy of the string S converted to lowercase.
        """
        return ""

        用法:返回字符串的副本,其中所有大寫字母都轉換為小寫字母。

    def swapcase(self): # real signature unknown; restored from __doc__
        """
        S.swapcase() -> str
        
        Return a copy of S with uppercase characters converted to lowercase
        and vice versa.
        """
        return ""

        用法:返回字符串副本,其中大小寫進行了互換。

    def title(self): # real signature unknown; restored from __doc__
        """
        S.title() -> str
        
        Return a titlecased version of S, i.e. words start with title case
        characters, all remaining cased characters have lower case.
        """
        return ""

        用發:返回字符串副本,其中單詞都已大寫字母開頭。


8.字符串條件判斷
    def isalnum(self): # real signature unknown; restored from __doc__
        """
        S.isalnum() -> bool
        
        Return True if all characters in S are alphanumeric
        and there is at least one character in S, False otherwise.
        """
        return False

    def isalpha(self): # real signature unknown; restored from __doc__
        """
        S.isalpha() -> bool
        
        Return True if all characters in S are alphabetic
        and there is at least one character in S, False otherwise.
        """
        return False

    def isdecimal(self): # real signature unknown; restored from __doc__
        """
        S.isdecimal() -> bool
        
        Return True if there are only decimal characters in S,
        False otherwise.
        """
        return False

    def isdigit(self): # real signature unknown; restored from __doc__
        """
        S.isdigit() -> bool
        
        Return True if all characters in S are digits
        and there is at least one character in S, False otherwise.
        """
        return False

    def isidentifier(self): # real signature unknown; restored from __doc__
        """
        S.isidentifier() -> bool
        
        Return True if S is a valid identifier according
        to the language definition.
        
        Use keyword.iskeyword() to test for reserved identifiers
        such as "def" and "class".
        """
        return False

    def islower(self): # real signature unknown; restored from __doc__
        """
        S.islower() -> bool
        
        Return True if all cased characters in S are lowercase and there is
        at least one cased character in S, False otherwise.
        """
        return False

    def isnumeric(self): # real signature unknown; restored from __doc__
        """
        S.isnumeric() -> bool
        
        Return True if there are only numeric characters in S,
        False otherwise.
        """
        return False

    def isprintable(self): # real signature unknown; restored from __doc__
        """
        S.isprintable() -> bool
        
        Return True if all characters in S are considered
        printable in repr() or S is empty, False otherwise.
        """
        return False

    def isspace(self): # real signature unknown; restored from __doc__
        """
        S.isspace() -> bool
        
        Return True if all characters in S are whitespace
        and there is at least one character in S, False otherwise.
        """
        return False

    def istitle(self): # real signature unknown; restored from __doc__
        """
        S.istitle() -> bool
        
        Return True if S is a titlecased string and there is at least one
        character in S, i.e. upper- and titlecase characters may only
        follow uncased characters and lowercase characters only cased ones.
        Return False otherwise.
        """
        return False

    def isupper(self): # real signature unknown; restored from __doc__
        """
        S.isupper() -> bool
        
        Return True if all cased characters in S are uppercase and there is
        at least one cased character in S, False otherwise.
        """
        return False
        
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM