数据类型之字符串

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

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

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

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

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.

strip:在string上执行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.

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.

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:按行分割。换行符包括 \n、\r、\r\n。如果 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\n\nde fg\rkl\r\n'.splitlines()
# ['ab c', '', 'de fg', 'kl']
'ab c\n\nde fg\rkl\r\n'.splitlines(True)
# ['ab c\n', '\n', 'de fg\r', 'kl\r\n']

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 '}').

参考文档

posted @ 2017-10-05 00:31  KeithTt  阅读(290)  评论(0编辑  收藏  举报