擅长:python、mysql、java
<p>你要找的是:</p>
<pre><code>>>> s[-7:1:-2]
'vka'
</code></pre>
<p>这也是:</p>
<pre><code>>>> s[6:1:-2]
'vka'
</code></pre>
<p>请注意,您总是从提供的第一个索引开始,一直到达到(或大于)提供的第二个索引为止。第三个指标是步幅。你知道吗</p>
<p>在您的例子中,您从8开始(它已经大于2),因此您以一个空字符串结束。你知道吗</p>
<p>我想你已经预料到了,如果步幅是负的,第一个索引应该是字符串右侧的位置。这不是从字符串左侧开始计算负索引的情况。你知道吗</p>
<p>像往常一样,<a href="https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange" rel="nofollow">the official language reference</a>比我能更正式(更正确)地陈述它。你知道吗</p>
<blockquote>
<p>The slice of s from i to j with step k is defined as the sequence of items with index x = i + n*k such that 0 <= n < (j-i)/k. In other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping when j is reached (but never including j). If i or j is greater than len(s), use len(s). If i or j are omitted or None, they become “end” values (which end depends on the sign of k). Note, k cannot be zero. If k is None, it is treated like 1.</p>
</blockquote>