Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Python String array work? [duplicate]

Tags:

python

string

i have a python string
word = helloworld
the answer for
word[1:9:2] will be given as "elwr". How this is happening? Thank you!!

like image 919
imageProcessingGuy Avatar asked May 16 '26 21:05

imageProcessingGuy


1 Answers

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.

like image 137
Óscar López Avatar answered May 19 '26 10:05

Óscar López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!