数据类型之字符串
字符串(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']
长度介于0和59字符串'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 '}').
参考⽂档

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。