Python String Slicing
What you will learn
- How to read a single character with an index, forwards or backwards
- How to cut a piece with a slice
[start:stop:step] - Shortcuts: leaving out
start,stoporstep, and reversing with[::-1] - Why a slice never causes an out-of-range error, but an index can
- Why you cannot assign to one character
1. Indexing: one character
Every character in a string has a position, called its index. Counting starts at 0:
s = "Pathkori"
print(s[0], s[1], len(s))
P a 8
| Character | P | a | t | h | k | o | r | i |
|---|---|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| Negative index | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
A negative index counts from the end: -1 is the last character, -2 the one before it.
print(s[-1], s[-2], s[len(s) - 1])
i r i
The index must be a whole number inside the string. Anything else is an error:
s[8]
IndexError: string index out of range
"abc"[1.0]
TypeError: string indices must be integers, not 'float'
Quick check: what is `"python"[-3]`?
"h". Counting from the end: n is -1, o is -2, h is -3.
2. Slicing: a piece of the string
A slice takes several characters at once. Write [start:stop]: it starts at start and stops just before stop.
s = "Pathkori"
print(s[0:4])
print(s[2:6])
Path
thko
The positions are like fence posts between characters:
P a t h k o r i
0 1 2 3 4 5 6 7 8
s[0:4] is everything between post 0 and post 4: Path. The length of the piece is stop - start. This "stop before" rule means that s[:4] + s[4:] always gives back the whole string.
Leaving parts out
Leave out start to begin at the start, and leave out stop to go to the end:
print(s[:4])
print(s[4:])
print(s[-3:])
print(s[:-3])
print(s[:])
Path
kori
ori
Pathk
Pathkori
| Slice | Meaning |
|---|---|
s[:4] |
the first 4 characters |
s[4:] |
everything from index 4 on |
s[-3:] |
the last 3 characters |
s[:-3] |
everything except the last 3 |
s[:] |
a full copy |
3. The step
A third number is the step: how far to move each time. The full form is [start:stop:step]:
print(s[::2])
print(s[1:7:2])
print("abcdef"[::3], "abcdef"[1::3])
Ptkr
aho
ad be
s[::2]takes every second character:P,t,k,r.s[1:7:2]starts at 1, stops before 7, moves 2 at a time:a,h,o.
A negative step goes backwards
print(s[::-1])
print(s[5:2:-1])
print("hello world"[::-1])
irokhtaP
okh
dlrow olleh
[::-1] is the standard way to reverse a string. With a negative step, start should be larger than stop: s[5:2:-1] walks from index 5 down to, but not including, index 2.
A step of 0 is an error: "abc"[::0] raises ValueError: slice step cannot be zero.
4. Slices are forgiving
An index outside the string is an error, but a slice is not. Python simply stops at the ends:
print(repr(s[10:]))
print(s[:100])
print(repr(s[6:2]))
''
Pathkori
''
Out-of-range slices give a shorter result, or an empty string, and never an error. That is convenient, but it can hide mistakes, so check unexpected empty results.
5. Slice objects
A slice can be given a name with slice(start, stop, step), which is handy if you use the same cut several times:
cut = slice(1, 5)
print(s[cut], "Hello World"[cut])
athk ello
6. You cannot change a character
Strings are immutable (lesson 2.6). You can read s[0], but you cannot assign to it:
s[0] = "p"
TypeError: 'str' object does not support item assignment
To "change" a character, build a new string from slices:
x = "hello"
print(x[0].upper() + x[1:])
print(x[:2] + "L" + x[3:])
Hello
heLlo
The first line capitalises the first letter. The second replaces the third letter, at index 2, by joining the part before it, the new letter and the part after it.
Worked example: split a date and check a palindrome
A date is stored as text "2026-10-20" (year, month, day, each at a fixed position).
date = "2026-10-20"
print(date[:4], date[5:7], date[-2:])
2026 10 20
| Part | Slice | Why |
|---|---|---|
| year | date[:4] |
the first four characters |
| month | date[5:7] |
indexes 5 and 6 (after the dash) |
| day | date[-2:] |
the last two characters |
A palindrome reads the same both ways. Reverse with [::-1] and compare:
word = "madam"
print(word == word[::-1])
print("hello" == "hello"[::-1])
True
False
Common mistakes
- Counting from 1. The first index is
0. - Expecting the stop index to be included.
s[0:4]has 4 characters, ending at index 3. - Using an index beyond the end, which gives
IndexError(a slice would not). - Trying to change a character with
s[0] = .... Build a new string instead. - Using a float as an index. Use
int(...)or//.
Key takeaways
- Indexes start at
0; negative indexes count from the end (-1is last). - A slice
s[start:stop:step]includesstartand excludesstop; leave parts out for the ends. s[::-1]reverses a string;s[-3:]is the last three characters.- Slices never raise an out-of-range error; indexes do.
- Strings cannot be changed in place; build a new string from slices.
Practice
1. With word = "banana", what are word[1], word[-1] and word[1:4]?
Show answer
a, a and ana (indexes 1, 2 and 3).
2. Write a slice for the first three letters and one for the last three letters of "programming".
Show answer
"programming"[:3] gives pro, and "programming"[-3:] gives ing.
3. What does "abcdef"[::2] give? And "abcdef"[1::2]?
Show answer
ace and bdf.
4. Use a slice to reverse "stressed".
Show answer
"stressed"[::-1] gives desserts.
5. What is the difference between "abc"[5] and "abc"[5:]?
Show answer
"abc"[5] raises an IndexError. "abc"[5:] gives an empty string '', because slices never raise an out-of-range error.
6. Turn "python" into "Jython" by building a new string.
Show answer
"J" + "python"[1:] gives Jython.