String Indexing & Slicing
Access parts of strings using indices and slices. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with First & Last CharacterStudy Material
Read the full lesson
Looking inside a string
A string is just a sequence of characters.
Often, you only want a specific piece of that sequence. Maybe you want the first letter of a user's name, or you want to extract the year from the date "2025-10-31".
Python allows you to grab individual characters, or chunks of characters, using a system called indexing and slicing.
Zero-based indexing
Every character in a string has a numbered position, called its index.
However, computers start counting at 0, not 1.
pythonword = "Python" print(word[0]) # Output: P print(word[1]) # Output: y print(word[5]) # Output: n
In the word "Python", the P is at index 0, and the n is at index 5.
If you try to grab an index that doesn't exist, like word[10], Python will crash with an IndexError.
Negative indexing
What if you have a very long paragraph, and you just want the very last character?
Counting all the way from the beginning would be slow and annoying.
Python provides a clever shortcut: negative numbers count backward from the end.
pythonword = "Python" print(word[-1]) # Output: n (the last character) print(word[-2]) # Output: o (the second to last character)
Negative indexing is incredibly useful because it works no matter how long the string is. string[-1] is always the last letter.
Grabbing chunks with slicing
If you want more than one character, you can "slice" the string.
You do this by providing a start and a stop index, separated by a colon :.
pythonword = "Python" print(word[0:3]) # Output: Pyt
Here is the most important rule about slicing: Python includes the start index, but excludes the stop index.
So 0:3 gives you index 0, 1, and 2. It stops right before hitting index 3.
Slicing shortcuts
If you are starting from the very beginning, you can leave the start index blank.
pythonprint(word[:4]) # Output: Pyth (From start up to index 4)
If you are going all the way to the end, you can leave the stop index blank.
pythonprint(word[2:]) # Output: thon (From index 2 to the end)
By combining these, word[:] actually gives you a full copy of the entire string!
Skipping characters with step
There is a third, optional number you can add to a slice: the step.
It tells Python how many characters to jump forward each time.
The full format is [start:stop:step].
pythonalphabet = "ABCDEFGHIJ" print(alphabet[0:10:2]) # Output: ACEGI
This starts at 0, stops at 10, and jumps by 2 characters at a time. It effectively grabs every other letter.
The ultimate shortcut: reversing a string
If you use a negative step, Python will walk backwards through the string.
This gives us one of Python's most famous and elegant tricks.
To completely reverse a string in Python, you slice it from beginning to end, with a step of -1.
pythontext = "racecar" reversed_text = text[::-1] print(reversed_text) # Output: racecar
What this lesson should give you
After this lesson, you should understand how to:
- grab a single character using its index number (like
text[0]) - remember that Python starts counting at 0
- grab the last characters easily using negative indexes (like
text[-1]) - slice out a chunk of text using
[start:stop] - understand that the stop index is excluded
- use the step parameter to skip characters or reverse strings
Indexing and slicing are not just for strings! You will use these exact same techniques when we learn about Lists later in the course.
Interactive
Exercises for this topic
These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.
First & Last Character
Complete the "First & Last Character" exercise.
Extract Substring
Complete the "Extract Substring" exercise.
Reverse a String
Complete the "Reverse a String" exercise.
Every Nth Character
Complete the "Every Nth Character" exercise.
Palindrome Checker
Complete the "Palindrome Checker" exercise.