• 数据类型之字符串


    字符串(string)是由零个或多个字符组成的有限序列,官方叫做Text Sequence Type,即文本序列。

    字符串通常以串的整体作为操作对象,字符串跟元祖一样也是不可变序列。

    字符串用引号包含标识,python中双引号和单引号的意义相同,都可用于表示字符串。

    python中的字符串分为两种:

    • 普通字符串,用引号声明。
    • Unicode字符串,在引号前面加上字母u声明。

    如果字符串中包含汉字,则应该将其声明为Unicode字符串。

    内置函数

    字符串作为最重要的常用数据类型之一,有一系列特有的内置函数,常用的如下:

    >>> s='abc'
    >>> dir(s)
    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    

    find:检测指定字符是否在string中(模糊查找,区分大小写),可以用start和end指定检测范围,一旦找到则返回索引值(注意是找到的第一个),如果没找到则返回-1。

    >>> help(s.find)
    Help on built-in function find:
    
    find(...) method of builtins.str instance
        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.
    
    >>> 'abc'.find('b')
    1
    >>> 'abc'.find('d')
    -1
    

    index:跟find类似,但是如果没检测到指定的字符,则会报ValueError异常。

    >>> help(s.index)
    Help on built-in function index:
    
    index(...) method of builtins.str instance
        S.index(sub[, start[, end]]) -> int
        
        Like S.find() but raise ValueError when the substring is not found.
    
    >>> 'abc'.index('b')
    1
    >>> 'abc'.index('d')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: substring not found
    

    strip:在srting上执行lstrip()和rstrip(),即删除string左边和右边的空格。如果带了参数,则可以删除参数指定字符。

    >>> help(s.strip)
    Help on built-in function strip:
    
    strip(...) method of builtins.str instance
        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.
    
    >>> ' abcabc '.strip()
    'abcabc'
    

    split:字符串分割,如果没有指定分割符,则以空白为分割符,空字符会被删除。还可以通过maxsplit指定分割次数。

    >>> help(s.split)
    Help on built-in function split:
    
    split(...) method of builtins.str instance
        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.
    
    >>> 'a-b-c'.split('-')
    ['a', 'b', 'c']
    

    join:连接字符串,注意连接符写在前面。

    >>> help(s.join)
    Help on built-in function join:
    
    join(...) method of builtins.str instance
        S.join(iterable) -> str
        
        Return a string which is the concatenation of the strings in the iterable.
        The separator between elements is S.
    
    >>> '-'.join('abcd')
    'a-b-c-d'
    
    >>> ','.join(map(str, range(10)))
    '0,1,2,3,4,5,6,7,8,9'
    

    partition:也是字符串分割,不同于split(),这里是寻找字符串中的分隔符,然后从第一个找到的分隔符处分割,返回一个元祖,包含三部分(前半段, 分隔符, 后半段)。这里必须要指定分隔符,如果不指定,将抛出TypeError。如果指定的分隔符未找到,则返回原字符串和两个空字符串。

    Help on built-in function partition:
    
    partition(...) method of builtins.str instance
        S.partition(sep) -> (head, sep, tail)
    
        Search for the separator sep in S, and return the part before it, the separator itself, and the part after it.  
        If the separator is not found, return S and two empty strings.
    
    >>> s = 'axbcxyz'
    >>> s.partition('x')
    ('a', 'x', 'bcxyz')
    
    >>> s.partition('m')
    ('axbcxyz', '', '')
    

    splitlines:按行分割。换行符包括 、 、 。如果keepends设置为True,则保留分隔符。

    Help on built-in function splitlines:
    
    splitlines(...) method of builtins.str instance
        S.splitlines([keepends]) -> list of strings
    
        Return a list of the lines in S, breaking at line boundaries.
        Line breaks are not included in the resulting list unless keepends is given and true.
    
    >>> 'ab c
    
    de fg
    kl
    '.splitlines()
    ['ab c', '', 'de fg', 'kl']
    >>> 'ab c
    
    de fg
    kl
    '.splitlines(True)
    ['ab c
    ', '
    ', 'de fg
    ', 'kl
    ']
    

    format:格式化字符串,这里的占位符是大括号{}。

    >>> help(s.format)
    Help on built-in function format:
    
    format(...) method of builtins.str instance
        S.format(*args, **kwargs) -> str
        
        Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}').
    

    字符串格式化是可以用%s、%d等占位符实现的,不过相比之下,s.format()更易读优雅。

    参考:
    https://docs.python.org/3/library/stdtypes.html#textseq
    https://docs.python.org/3/library/string.html
    http://www.runoob.com/python3/python3-string.html
    http://www.runoob.com/python/att-string-format.html

  • 相关阅读:
    ESB数据发布思路
    Mongo客户端MongoVUE的基本使用
    ESB数据采集思路
    css3学习笔记之按钮
    发布Qt程序时别忘了带上plugins(codecs等)
    [Qt]Cannot retrieve debugging output
    Linux下编译opencv库[转]
    OpenCV库Windows下QT编译及使用
    ipad4自动下载了ios8的安装包,好几个G啊,不想更新,怎么删了呢?
    VC: error LNK2019:unresolved external symbol *** referenced in function ***的解决方案
  • 原文地址:https://www.cnblogs.com/keithtt/p/7628116.html
Copyright © 2020-2023  润新知