I am currently trying out slicing a string in Python and was confused about how exactly does Python interpret the term 'None' because it behaves rather conveniently when used but I am not how does it select the indexes when used.
I have no doubts about how to use slice, but about HOW it works. None of the questions I have visited (for example) explain what index does 'none' refer to for each case mentioned below.
I got this question because I was playing around with negative indexing in Python;
str="Python"
print(str[-len(str):-1])
Will give us the output; Pytho
If we try replacing the -1 with a 0 it obviously returns an empty string. However if we replace it with None, it will return the whole string
str="Python"
print(str[-len(str):])
Will give us the output Python
Even when we do this
str="Python"
print(str[:])
It will return the whole string!
I was wondering what Index does the 'None' refer to in each case above?
None, 0 and-len(str)are equivalent (see that answer in the duplicate question about-len(str)), for the endNoneandlen(str)are equivalent. And the second reference for duplicate also explain well in a short way