I'm new to Python, and I find the slice behaviour somewhat confusing.
If I do
test = 'abcdefgh'
for i in range(7):
print test[-(8-i):-(6-i)]
print i
the last iteration will misbehave. Since slicing [start:end] doesn't include end, it seems to me like I'd need to handle slices like this with a special case if the last character is in the range I want.
Did I miss something?
"a"[0:100000] == "a". You do not getIndexErrorwhen using slices for out-of-range indexes. The out-of-range index is either replaced with the end/the beginning or, in other circumstances, the result is an empty string.[-2:1]returns an empty string while[-2:0]works? One thing is if you omit thestopparameter, an other thing is if you provide an explicit index.s, it always holds that:s[x:y] in sisTrue, no matter whatxandyare of if they are omitted; In other wordss[x:y]is always a substring ofs(if you omit the step). Including the changes you suggest would break this invariant. I believe this is the reason why they didn't want to implicitly change the step in these circumstances.s[x:y]should always return a substring ofs, not a substring ofsor the inverse ofsdepending on the indeces.