[Tutor] String slicing off the end question

David Rock david at graniteweb.com
Tue Dec 22 11:46:25 EST 2020


I was just doing some string slicing, and came across a behavior I did not expect.

>>> somestring = 'python'

>>> somestring[:2]
'py'

>>> somestring[:10]
'python'

In particular, I was surprised by defining a slice outside the range not
throwing an exception (eg, in the example above, the string is length 6,
but setting a stop of 10 doesn't have any issues and appears to just
return the whole string).  Is this how it's supposed to work? If it is,
then why?  Is this behavior reliable?

Thanks. 

-- 
David Rock
david at graniteweb.com


More information about the Tutor mailing list