0

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?

3
  • this might be relevant mathspp.com/blog/pydonts/inner-workings-of-sequence-slicing Commented Jun 26 at 7:31
  • yes you can substitute None for any of the empty spaces, that allows to use for instance variables to specify start or end. If you have doubt when start and/or end is missing (or None) refer to the link in the comment above Commented Jun 26 at 7:43
  • 3
    what Index does the 'None' refer to in each case above : for the start position None, 0 and -len(str) are equivalent (see that answer in the duplicate question about -len(str) ), for the end Noneand len(str) are equivalent. And the second reference for duplicate also explain well in a short way Commented Jun 26 at 8:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.