字符串索引
字符串可编入索引(或带下标)。 字符串的第一个字符的索引为 0。 没有单独的字符类型。 字符只是大小为 1 的字符串。 下面介绍如何获取特定索引处的字符。
word = 'Python'
word[0] # Character in position 0.
输出为:
'P'
指定其他索引值来返回该位置中的字符:
word[5] # Character in position 5.
输出为:
'n'
索引也可以是负数,表示计数从字符串的末尾开始。 由于 -0 相当于 0,因此负索引从 -1 开始:
word[-1] # Last character.
输出为:
'n'
同样地,其他负索引会从相应的位置返回字符:
word[-2] # Second-to-last character.
输出为:
'o'
切片
Python 既支持索引,也支持切片,前者从字符串中提取单个字符,后者提取子字符串(或切片)。 若要进行切片,需采用“开始:结束”格式指示范围。 开始位置包含在返回的子字符串中,但不包括结束位置:
word[0:2] # Characters from position 0 (included) to 2 (excluded).
输出为:
'Py'
下面是指定不同范围的另一示例:
word[2:5] # Characters from position 2 (included) to 5 (excluded).
输出为:
'tho'
如果省略任一位置,则默认开始位置为 0,默认结束位置为字符串的长度:
word[:2] # Characters from the beginning to position 2 (excluded).
输出为:
'Py'
下面是省略了结束位置的示例:
word[4:] # Characters from position 4 (included) to the end.
输出为:
'on'
此示例显示在开始位置使用负索引:
word[-2:] # Characters from the second-to-last (included) to the end.
输出为:
'on'
该特征表示 s[:i] + s[i:] 始终等于 s,如下所示:
word[:2] + word[2:]
输出为:
'Python'
如果为 i 提供其他索引值,结果是相同的:
word[:4] + word[4:]
输出为:
'Python'
要记住切片的工作原理,一种方式是将索引视为指向字符之间。 字符串中第一个字符的左边缘数字为 0。 可通过索引 i 从此位置切片。 如果字符串包含 n 个字符,则其最后一个字符的右边缘具有索引 n。 可通过索引 j 从此位置切片。 例如:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
+---+---+---+---+---+---+
-6 -5 -4 -3 -2 -1
+---+---+---+---+---+---+
i j
+---+---+---+---+---+---+
在我们的示例中:
- 第一行包含我们要切片的字符串“Python”。
- 第二行显示索引 0 到 6 在字符串中的位置。
- 第三行显示相应的负索引。 请注意,此行中没有 0。
- 第四行显示切片索引
i和j的开始位置。
从 i 到 j 的切片包括分别标记为 i 和 j 的边缘之间的所有字符。
对于非负索引,切片的长度是索引之间的差值(如果两者都在边界内)。 例如,word[1:3] 的长度为 2。
大型索引值
如果指定的索引值太大,则会出现错误:
word[42] # The word only has 6 characters.
错误输出为:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-70-e894f93573ea> in <module>
----> 1 word[42] # The word only has 6 characters.
IndexError: string index out of range
但在范围内,太大的索引值会默认为字符串的大小,不会导致错误。 如果你总是希望在特定索引处开始切片,则无论字符串的长度如何,该特征都很有用:
word[4:42]
输出为:
'on'
此示例显示如果切片的开始索引大于字符串的长度,会发生什么情况:
word[42:]
输出为:
''
字符串不可变性
Python 字符串是不 可变的,这意味着它们无法更改。 因此,向字符串中的索引位置赋值会导致错误:
word[0] = 'J'
错误输出为:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-73-91a956888ca7> in <module>
----> 1 word[0] = 'J'
TypeError: 'str' object does not support item assignment
以下单元也会生成错误:
word[2:] = 'py'
错误输出为:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-74-6488bbf78f5a> in <module>
----> 1 word[2:] = 'py'
TypeError: 'str' object does not support item assignment
切片本身是一个值,可通过加号(“+”)运算符将它与其他值串联:
'J' + word[1:]
输出为:
'Jython'
再提供一个示例:
word[:2] + 'Py'
输出为:
'PyPy'
但是,切片不是字符串文本。 它不可用于自动串联。 下面的代码会生成错误:
word[:2] 'Py' # Slice is not a literal; produces an error.
输出为:
File "<ipython-input-77-60be1c701626>", line 1
word[:2] 'Py' # Slice is not a literal; produces an error.
^
SyntaxError: invalid syntax
计算字符串的长度通常很有用。 内置函数 len() 返回字符串的长度:
s = 'supercalifragilisticexpialidocious'
len(s)
输出为:
34
另一个用于处理字符串的内置函数是 str() 。 此函数采用任何对象,并返回该对象的可打印字符串版本。 例如:
str(2)
输出为:
'2'
再提供一个示例:
str(2.5)
输出为:
'2.5'
要点
对字符串数据的操作是在 Python 中的数据科学中执行的其他任务的基础。 现在熟悉字符串会在你稍后处理日益复杂的数据时提供很大的帮助。