can someone please explain to me why this gives empty string as its output and not 'oht';

word='python'

word[-2:-5]

1 Answers

2
Majo_Jose On

You will get your expected result by running the below Code

>>word[-2:1:-1]
'oht'

Usually, The slicing happens from left to right. For Example,

word='python'
word[-2:]

Results

'on'

word[:-5]

Will Results

'p'

While Doing to attempt it together

word[-2:-5]

There will be no output to give you a result. The first index should be smaller than the second index. in your question -2 is greater than -5.