First page Back Continue Last page Overview Graphics

Indexing and Slicing

-11

-10

-7

-8

-9

-6

-5

-2

-3

-4

-1

0

1

4

3

2

5

6

9

8

7

10

Indices can be accessed from backwards as well:

>>> txt[-1]

'd'

>>> txt[-5]

'W'

'Hello'

>>> txt[0:-6]

'Hello'

>>> txt[:5]

'Hello'

>>> txt[6:]

'World'

Particular Elements of a String (characters) can be indexed and accessed with square brackets:

>>> txt = "Hello World"

>>> txt[0]

'H'

>>> txt[4]

'o'

Substrings:

>>> txt = "Hello World"

>>> txt[1:5]

'ello'

>>> txt[0:5]

'Hello'

H

e

o

l

l

W

l

r

o

d

Jedes n-te Zeichen:

>>> txt[::2]

'HloWrd'

>>> txt[::3]

'HlWl'

String umkehren:

>>> txt[::-1]

'dlroW olleH'

>>>