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!!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With