[Tutor] Questions regarding strings

Hugo Arts hugo.yoshi at gmail.com
Sun Aug 8 19:13:02 CEST 2010

On Sun, Aug 8, 2010 at 12:04 PM, Daniel <asmosis.asterix at gmail.com> wrote:
> Hello everyone! I would like to ask you two questions regarding strings
> which I do not know. Excuse me in advance if the questions may seem a bit
> dumb. I'm a beginner. So let's get back to the point, this is my string:
> msg= 'Hello world'
> If I do, msg[:3] I get the following output, 'Hel'
> If I do, msg[6:] I get the following output, 'World'
> Ok, so I understand why this is happening. What I do not understand is why I
> get the following output in this cases.
> 1) msg[:-1] which has the output 'Hello Worl'

negative indexes start from the end of the string. So msg[-1] == 'd',
and the slice there means "start at the first character, and go up to
the last.

> 2) msg[0:6:2]  'Hlo'

This means start at the first, up to the sixth, making steps of 2.
Steps of two means every other letter is skipped, of course.

> 3) msg[::-1] 'dlroW olleH'

This slice catches the whole string, but makes steps of -1, which
essentially means you'll go through the strings backwards.


More information about the Tutor mailing list