[Tutor] slicing a string

Andre Engels andreengels at gmail.com
Tue Sep 7 00:52:58 CEST 2010


On Tue, Sep 7, 2010 at 12:44 AM, lists <lists at justuber.com> wrote:
>>> Assuming that mytext is "test", I've found that mytext[-1:-4:-1]
>>> doesn't work (as I expected it to) but that mytext[::-1] does.
>>>
>>> While that's fine, I just wondered why mytext[-1:-4:-1] doesn't work?
>>
>> How does it not "work"? What did you expect to happen? What did it do instead?
>>
>> Greets
>> Sander
>>
>
> Hi, assuming mytext is "test", word[-1:-4:-1] returns tse
>
> My understanding of how the index works on test would be:
>
> 0  1  2  3
> t   e  s   t
> -4 -3 -2 -1
>
> So I just wasn't clear on what happened to the last 't' I expected to see.


>>> "test"[0:3]
'tes'

[m:n] shows the elements from m upto but excluding n.


-- 
André Engels, andreengels at gmail.com


More information about the Tutor mailing list