Indexing & Slicing
Learn how to grab any character or chunk from a string using Python's powerful indexing and slicing syntax.
Imagine a string as a row of numbered seats in a cinema. Every character has its own seat number — a position — and you can reach into any seat to grab what's sitting there. This is called indexing. You can also say "give me seats 2 through 5" to grab a whole section at once. That's called slicing. Both skills are incredibly useful any time you work with text.
See it in action
— step through the idea, then dive into the details below.Pick Any Character from a String
A string is like a row of numbered seats — every character has a position. Use indexing to grab one character, or slicing to grab a whole chunk.
#Positions Start at Zero
Python numbers positions starting from 0, not 1. So the very first character is at position 0, the second at position 1, and so on. This surprises almost every beginner — but you'll get used to it quickly.
word = "Python"
print(word[0]) # first character
print(word[1]) # second character
print(word[5]) # sixth (last) characterThe Elevator Analogy
In many countries, the ground floor of a building is called floor 0, not floor 1. Python counts characters the same way — the ground floor is 0. Once that clicks, everything else follows naturally.
#Negative Indexes Count from the End
Python also lets you count backwards from the end using negative numbers. -1 is always the last character, -2 is second-to-last, and so on. This is handy when you don't know how long a string is.
word = "Python"
print(word[-1]) # last character
print(word[-2]) # second-to-last
print(word[-6]) # same as word[0]Index Out of Range
If you use an index that doesn't exist — like word[99] on a 6-letter string — Python raises an IndexError and your program crashes. Always make sure your index is within bounds!
``python word = "Python" print(word[99]) # IndexError: string index out of range ``
#Slicing: Grabbing a Range
Instead of one seat, a slice grabs a whole range of characters. The syntax is s[start:stop]. Python returns the characters from position start up to — but not including — position stop. The stop being exclusive is the trickiest part, so read that again slowly.
word = "Python"
print(word[0:3]) # positions 0, 1, 2 (not 3)
print(word[2:5]) # positions 2, 3, 4A Quick Way to Remember
Think of the numbers as gaps between characters, not the characters themselves. Position 0 is the gap before P, position 1 is the gap before y, etc. word[0:3] means "cut at gap 0 and gap 3" — everything between those cuts is returned.
#Omitting Start or Stop
You can leave out start or stop and Python will fill in a sensible default. Omitting start means "begin at the very beginning". Omitting stop means "go all the way to the end".
word = "Python"
print(word[:3]) # from start up to (not including) position 3
print(word[2:]) # from position 2 to the end
print(word[:]) # the whole string — a full copy#The Step Value
Slices have a third optional value: s[start:stop:step]. The step controls how many positions to jump between each character you pick. The default step is 1 (take every character). A step of 2 takes every other character.
word = "Python"
print(word[0:6:2]) # every 2nd character
print(word[::3]) # every 3rd character from start to end#Reversing a String with [::-1]
A step of -1 walks backwards through the string. Combined with omitted start and stop, s[::-1] is the classic Python one-liner for reversing a string.
word = "Python"
print(word[::-1]) # reversed
greeting = "Hello!"
print(greeting[::-1])Slices Never Crash
Unlike single-index access, slices never raise an IndexError. If your start or stop is out of bounds, Python just returns whatever it can.
``python word = "Hi" print(word[0:100]) # no crash — returns 'Hi' print(word[50:]) # no crash — returns '' ``
This makes slicing much safer for situations where you're not sure of the length.
#This Works on Lists Too
Everything you just learned transfers directly to lists. Lists use the exact same [index] and [start:stop:step] syntax. So once you're comfortable slicing strings, you already know how to slice lists — which you'll see in a later lesson.
colors = ["red", "green", "blue", "yellow", "purple"]
print(colors[0]) # first item
print(colors[-1]) # last item
print(colors[1:3]) # a slice of the list
print(colors[::-1]) # reversed listWhat does `"abcde"[1:4]` return?
Key takeaways
- Python indexes start at 0 — the first character is always at position 0.
- Negative indexes count from the end: -1 is the last character.
- Slicing with s[start:stop] returns characters from start up to (not including) stop.
- Omit start or stop to slice from the beginning or to the end; add a step with s[start:stop:step].
- s[::-1] reverses any string, and slices never raise IndexError unlike single-index access.
What does this code print?
word = "Python"
print(word[0])
print(word[-1])
print(word[1:4])This code is meant to print the last 3 characters of the string, but it has a bug. What is wrong?
word = "Python"
print(word[3:6])
print(word[-3])Complete the code so it prints the word reversed: nohtyP
word = "Python" print(word[])
Put these lines in the right order to print every other character of a word, then print it reversed.
print(word[::-1])
word = "abcdef"
print(word[::2])
You're given the string sentence = "The quick brown fox". Using only indexing and slicing (no other string methods), do the following: 1. Print the first character. 2. Print the last character. 3. Print the first three characters. 4. Print the word "fox" (the last 3 characters). 5. Print the whole sentence reversed.
Try it live — edit the code and hit Run to execute real Python: