Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Indeksering og Udskæring | Strenge
Datatyper i Python

bookIndeksering og Udskæring

Strings are sequences: each character has a position (an index). Python uses zero-based indexing, so the first character is at index 0. You can take single characters with indexing and ranges of characters with slicing.

Indexing

Use square brackets with a single position.

123
s = "python" print(s[0]) # 'p' (first character) print(s[5]) # 'n' (sixth character)
copy

Negative indices count from the end.

123
s = "python" print(s[-1]) # 'n' (last character) print(s[-2]) # 'o' (second from the end)
copy

Indexing must hit an existing position, otherwise you get IndexError.

12
s = "python" print(s[10]) # IndexError: string index out of range
copy

Also, strings are immutable, so you can read s[i] but not assign to it.

12
s = "python" s[0] = 'P' # TypeError: 'str' object does not support item assignment
copy

Slicing

A slice uses start:stop:step and returns a new string. stop is exclusive (it's not included).

12345
s = "python" print(s[1:4]) # 'yth' (indices 1,2,3) print(s[:4]) # 'pyth' (start defaults to 0) print(s[3:]) # 'hon' (stop defaults to len(s)) print(s[::2]) # 'pto' (every 2nd character)
copy

Slices are forgiving: going past the ends just trims to valid bounds (no error).

12
s = "python" print(s[0:100]) # 'python'
copy

Negative Indices and Reversing

You can mix negative indices in slices, and a negative step walks backward.

123
s = "python" print(s[-3:]) # 'hon' (last three) print(s[::-1]) # 'nohtyp' (reverse)
copy
Note
Note

step cannot be 0. Leaving out step implies 1. Leaving out start or stop means "from the beginning" / "to the end".

1. What value will this code output?

2. What value will this code output?

3. Which statement raises an error for u = "hello"?

question mark

What value will this code output?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

question mark

Which statement raises an error for u = "hello"?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain more about how negative indexing works in Python?

What happens if I try to access an index that doesn't exist in the string?

Can you show more examples of slicing with different step values?

bookIndeksering og Udskæring

Stryg for at vise menuen

Strings are sequences: each character has a position (an index). Python uses zero-based indexing, so the first character is at index 0. You can take single characters with indexing and ranges of characters with slicing.

Indexing

Use square brackets with a single position.

123
s = "python" print(s[0]) # 'p' (first character) print(s[5]) # 'n' (sixth character)
copy

Negative indices count from the end.

123
s = "python" print(s[-1]) # 'n' (last character) print(s[-2]) # 'o' (second from the end)
copy

Indexing must hit an existing position, otherwise you get IndexError.

12
s = "python" print(s[10]) # IndexError: string index out of range
copy

Also, strings are immutable, so you can read s[i] but not assign to it.

12
s = "python" s[0] = 'P' # TypeError: 'str' object does not support item assignment
copy

Slicing

A slice uses start:stop:step and returns a new string. stop is exclusive (it's not included).

12345
s = "python" print(s[1:4]) # 'yth' (indices 1,2,3) print(s[:4]) # 'pyth' (start defaults to 0) print(s[3:]) # 'hon' (stop defaults to len(s)) print(s[::2]) # 'pto' (every 2nd character)
copy

Slices are forgiving: going past the ends just trims to valid bounds (no error).

12
s = "python" print(s[0:100]) # 'python'
copy

Negative Indices and Reversing

You can mix negative indices in slices, and a negative step walks backward.

123
s = "python" print(s[-3:]) # 'hon' (last three) print(s[::-1]) # 'nohtyp' (reverse)
copy
Note
Note

step cannot be 0. Leaving out step implies 1. Leaving out start or stop means "from the beginning" / "to the end".

1. What value will this code output?

2. What value will this code output?

3. Which statement raises an error for u = "hello"?

question mark

What value will this code output?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

question mark

Which statement raises an error for u = "hello"?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2
some-alt