通过Python中的数组语法寻址字符串的一部分

lam*_*mas 3 python string

在Python中,是否可以通过标准数组语法处理字符串中的特定字符?

例如,PHP:

$foo = 'bar';
echo $foo[1]; // Output: a
Run Code Online (Sandbox Code Playgroud)

它不像在PHP中那样工作,所以我想知道是否可以使用其他方式?

Mar*_*ers 5

正如Adam指出的那样,使用索引语法可以在Python中读取字符串数组.但是,使用以下语法写入字符串是不可能的:

>>> s = 'bar'
>>> s[2] = 'z'
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Run Code Online (Sandbox Code Playgroud)

也许这是你遇到的问题?