While I was studying string slicing, skipping of characters of a string and inverting a given string, I stumbled upon the following doubt:
var = 'abcdefghijklmnopqrstuvwxyz'
print(var[-20:-4]) # Works # '-4' is w
print(var[-4:-20]) # '-20' is g
print(var[-20:-4:1]) # Works # '-19' is h
print(var[-20:-4:-1]) #'-5' is v
print(var[-4:-20:1])
print(var[-4:-20:-1]) # Works
Here out of the 6 print
commands, only 3 of them worked and gave an output.
Output:
ghijklmnopqrstuv
(No output just a blank line)
ghijklmnopqrstuv
(No output just a blank line)
(No output just a blank line)
wvutsrqponmlkjih
Q1) Why didn't the other work. Which format works and which doesn't.
Q2) Before this doubt I used to believe that the first 2 numbers (-4 and -20 in this example) were supposed to be entered in ascending order, and that is true as we can see in the very first print
command. But then when I try to invert the first command by adding a -1 after the colon, it does not produce any output. What could be the reason behind this?
If you have any other tips on how to invert strings without errors please share.