擅长:python、mysql、java
<p>首先,数组索引是用整数而不是字符串来完成的:</p>
<pre><code>>>> a = [1, 2, 3]
>>> a["0"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> a[0]
1
</code></pre>
<p>其次,您可以在不使用太多<code>+</code>的情况下生成字符串:</p>
^{pr2}$
<p>第三,可以迭代字符串而不必将其转换为列表:</p>
<pre><code>>>> for c in list("abc"):
... print c
...
a
b
c
>>> for c in "abc":
... print c
...
a
b
c
</code></pre>
<p>第四,遍历一个字符串可以得到一个包含每个字符的字符串。您不必获取该字符串的第0个索引-这是相同的:</p>
<pre><code>>>> "b"
'b'
>>> "b"[0]
'b'
>>> "b"[0] == "b"
True
</code></pre>