字符串和分片
>>> s = "mary had a little lamb"
>>> s[0] # index is zero-based
'm'
>>> s[3] = 'x' # changing element in-place fails
Traceback (innermost last):
File "", line 1, in ?
TypeError: object doesn't support item assignment
>>> s[11:18] # 'slice' a subsequence
'little '
>>> s[:4] # empty slice-begin assumes zero
'mary'
>>> s[4] # index 4 is not included in slice [:4]
' '
>>> s[5:-5] # can use "from end" index with negatives
'had a little'
>>> s[:5]+s[5:] # slice-begin & slice-end are complimentary
'mary had a little lamb'
另一个功能强大的字符串操作就是简单的 in 关键字。它提供了两个直观有效的构造:
in 关键字
>>> s = "mary had a little lamb"
>>> for c in s[11:18]: print c, # print each char in slice
...
l i t t l e
>>> if 'x' in s: print 'got x' # test for char occurrence
...
>>> if 'y' in s: print 'got y' # test for char occurrence
...
got y
三重引号的使用
>>> s2 = """Mary had a little lamb
... its fleece was white as snow
... and everywhere that Mary went
... the lamb was sure to go"""
>>> print s2
Mary had a little lamb
its fleece was white as snow
and everywhere that Mary went
the lamb was sure to go
cStringIO 模块
>>> import cStringIO
>>> fh = cStringIO.StringIO()
>>> fh.write("mary had a little lamb")
>>> fh.getvalue()
'mary had a little lamb'
>>> fh.seek(5)
>>> fh.write('ATE')
>>> fh.getvalue()
'mary ATE a little lamb'
string 用法例 2
>>> import string
>>> s = "mary had a little lamb"
>>> string.capwords(s)
'Mary Had A Little Lamb'
>>> string.replace(s, 'little', 'ferocious')
'mary had a ferocious lamb'
还有许多没有在这里具体说明的其它转换;可以在 Python 手册中查找详细信息。
还可以使用 string 函数来报告字符串属性,如子串的长度或位置,例如:
string 用法例 3
>>> import string
>>> s = "mary had a little lamb"
>>> string.find(s, 'had')5>>> string.count(s, 'a')4
string 用法例 4
>>> import string>>> s = "mary had a little lamb"
>>> L = string.split(s)
>>> L
['mary', 'had', 'a', 'little', 'lamb']
>>> string.join(L, "-")
'mary-had-a-little-lamb'
当然,除了 .join() 之外,也许会利用列表来做其它事(如某些涉及我们熟悉的 for ... in ... 结构的事情)。
标准模块:re
re 模块废弃了在老的 Python 代码中使用的 regex 和 regsub 模块。虽然相对于 regex 仍然有几个有限的优点,不过这些优点微不足道,不值得在新代码中使用。过时的模块可能会从未来的 Python 发行版中删除,并且 1.6 版可能有一个改进的接口兼容的 re 模块。所以,规则表达式仍将使用 re 模块。