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.

        """

        return0

    用法:返回字符串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.

        """

        return0

        用法:返回字符串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.

        """

        returnFalse

        用法:检测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.

        """

        returnFalse

        用法:检测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个(从左往右替换)。如果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.

        """

        return0

        用法:计算字符串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.

        """

        returnFalse

    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.

        """

        returnFalse

    def isdecimal(self): # real signature unknown; restored from __doc__

        """

        S.isdecimal() -> bool


        Return True if there are only decimal characters in S,

        False otherwise.

        """

        returnFalse

    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.

        """

        returnFalse

    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".

        """

        returnFalse

    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.

        """

        returnFalse

    def isnumeric(self): # real signature unknown; restored from __doc__

        """

        S.isnumeric() -> bool


        Return True if there are only numeric characters in S,

        False otherwise.

        """

        returnFalse

    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.

        """

        returnFalse

    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.

        """

        returnFalse

    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.

        """

        returnFalse

    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.

        """

        returnFalse

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,258评论 0 10
  • str.capitalize(self)首字母变大写 返回值:S.capitalize() -> strin...
    TimeSHU阅读 441评论 0 0
  • 在读《如何阅读一本书》之前,我都是从头至尾一点一点得看下去,摘要通常被我忽略,序通常看得也不是非常认真,总是...
    墨迹2016阅读 389评论 0 0
  • 时间一点一滴消逝,现下的每一秒,转瞬就成了我回不去的曾经。 当一个人需要靠别人的苦难来成就自己的幸福,当一个人费尽...
    深夜芝士阅读 250评论 0 0
  • 王者荣耀真是火!某业务往来单位派人来馆,请新调整分管该项目的某主任接待一下,其人抱着手机躺坐在沙发上,眼睛全神贯注...
    青山有木阅读 179评论 0 0