0

I have a simple two lines of code but fail to understand what is actually happening.

    s = 'We\'re' + 'Here'
    print(s[4::2])

The result is just eee. I fail to see how the [4::2] is working to cause the code to print eee.

1 Answers1

1

It is something like [start: end: step], slice addresses can be written as s[start:end:step] and any of start, stop or end can be dropped. So, s[4::2] is starting from 4th and every 2nd element of the sequence(Here you dropped the end part). That's why it is returning eee

s = 'We\'re' + 'Here'
print(s[4::2])
Always Sunny
  • 32,751
  • 7
  • 52
  • 86