How Python String array work?
This question already has an answer here:
You're asking for an explanation of Python's slice notation. See this answer for details. In particular, notice that:
word = 'helloworld'
word[1:9:2]
... Is stating that a new slice should be created, beginning at index 1, up to (and not including) index 9, taking one element every two indexes in the string. In other words, create a new string with the following elements:
0 1 2 3 4 5 6 7 8 9
h e l l o w o r l d
^ ^ ^ ^
... And that's how you obtain 'elwr'
as a result.
This means that you are taking a sub-string from position[1] to position[9] and in that you are taking only the 2nd letter. Sub-string would be something like :
elloworld
and since you are taking the character at index 2 from that it would be :
elwr
Also its not an array. Its just a string.
array[begin:end:step]
word[1:9:2]
means you begin at index 1, and up until index 9, take every second letter.
上一篇: Python列表/数组:禁用负向索引换行
下一篇: Python字符串数组如何工作?