Indexing lets you get a single character from a string, and slicing lets you get a whole piece of it. Together, they’re how you grab the first letter of a name, the last four digits of a card number, the file extension of a path, or a reversed copy of a word.
In this tutorial, you’ll learn how Python numbers the characters in a string, how the [start:stop:step] syntax works in every combination, and how to avoid the off-by-one mistakes that confuse beginners.
Key terms used in this article:
- Index: The position number of a character. Python starts counting at
0. - Negative index: A position counted from the end of the string.
-1is the last character. - Slice: A part of a string selected with
[start:stop:step]. - Step: How many positions to move between characters. A negative step moves backward.
Table of contents
- How Python indexes a string
- Accessing characters with indexing
- Slicing syntax: [start:stop:step]
- Omitting start and stop
- Slicing with negative indexes
- Using a step
- Negative step: slicing backward
- Out-of-range slices don’t raise errors
- Slices create new strings
- The slice() object
- Slicing works on all sequences
- Real-world examples
- Quick reference
- Common mistakes
- Practice exercises
- Summary
How Python indexes a string
Every character in a string has two index numbers: a positive index counted from the left starting at 0, and a negative index counted from the right starting at -1.
String: P Y N A T I V E Positive index: 0 1 2 3 4 5 6 7 Negative index: -8 -7 -6 -5 -4 -3 -2 -1
The last positive index is always len(string) - 1.
Accessing characters with indexing
Use square brackets with an index to get a single character:
Output:
P Y E E I E
The result is always a string of length 1, because Python has no separate character type.
IndexError: string index out of range
An index outside the string raises an IndexError:
Output:
Error: string index out of range
word has 4 characters, so the valid indexes are 0 to 3 (or -1 to -4). This is the most common indexing mistake.
Looping over indexes
When you need both the position and the character, use enumerate() instead of range(len(...)):
Output:
0 l 1 o 2 o 3 p
Slicing syntax: [start:stop:step]
A slice returns a new string containing part of the original:
- start: The index where the slice begins. It’s included. Default: the beginning.
- stop: The index where the slice ends. It’s excluded. Default: the end.
- step: How many characters to move each time. Default:
1.
The rule “start is included, stop is excluded” means s[a:b] contains exactly b - a characters (when 0 <= a <= b <= len(s)).
Output:
Python Slicing tho 3
A helpful way to picture it: indexes point between characters, and a slice cuts at those points.
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1
"Python"[1:4] cuts at positions 1 and 4, giving "yth".
Omitting start and stop
Leave out start to slice from the beginning, and leave out stop to slice to the end:
Output:
Hello World Hello, World
s[:n] + s[n:] always gives back the original string s, for any n. This makes it easy to split a string at a position:
Output:
True 'Hello' ', World'
Slicing with negative indexes
Negative indexes are perfect for taking characters from the end without knowing the length:
Output:
pdf summary_report report
| Slice | Meaning |
|---|---|
s[:3] | First 3 characters |
s[-3:] | Last 3 characters |
s[3:] | Everything except the first 3 |
s[:-3] | Everything except the last 3 |
s[1:-1] | Everything except the first and last character |
Output:
abc fgh defgh abcde bcdefg
Using a step
The third value, step, skips characters:
Output:
acegikmoqsuwy bdfhjlnprtvxz afkpuz cfil
A step of 0 raises ValueError: slice step cannot be zero.
Negative step: slicing backward
With a negative step, Python walks from right to left. The defaults flip too: start defaults to the end and stop defaults to the beginning.
Output:
nohtyP nhy oht noh
With a negative step, start must be greater than stop, or the slice is empty:
Output:
'' 'oht'
Reversing a string
[::-1] is the standard way to reverse a string:
Output:
asseJ True
Out-of-range slices don’t raise errors
Unlike indexing, slicing is forgiving. If the indexes go past the ends of the string, Python just stops at the boundary:
Output:
short '' sho
This is convenient: text[:10] safely gives “up to 10 characters”, even for shorter strings.
Slices create new strings
Strings are immutable, so a slice always creates a new string. You can’t assign to a slice of a string, but you can build a new string from slices:
Output:
Error: 'str' object does not support item assignment Jathon I love learning Python abdef
The slice() object
The built-in slice() function creates a reusable slice. text[slice(start, stop, step)] is the same as text[start:stop:step]:
Output:
2026 12 04 hon
Named slices make code that parses fixed-width text much easier to read.
Slicing works on all sequences
Everything in this tutorial also works on lists, tuples, range objects, and bytes:
Output:
[20, 30, 40] [50, 40, 30, 20, 10] (1, 3) range(2, 8, 2)
The one difference: lists are mutable, so you can assign to a list slice, such as numbers[1:3] = [0, 0]. See Python Lists.
Real-world examples
Example 1: Get a file extension and name
Output:
vacation_photo jpeg
Example 2: Mask an email address
Output:
j********@example.com
Example 3: Show the last 4 digits of an account number
Output:
********9012
Example 4: Truncate long text for a preview
Output:
Short title A very long title...
Example 5: Rotate a string
Output:
cdefab efabcd
Example 6: Split a string into fixed-size chunks
Output:
['ABCD', 'EFGH', 'IJKL'] ABCD-EFGH-IJKL
Quick reference
| Expression | Result for s = "Python" | Meaning |
|---|---|---|
s[0] | 'P' | First character |
s[-1] | 'n' | Last character |
s[1:4] | 'yth' | Index 1 up to (not including) 4 |
s[:2] | 'Py' | First 2 characters |
s[2:] | 'thon' | From index 2 to the end |
s[-2:] | 'on' | Last 2 characters |
s[:-2] | 'Pyth' | All except the last 2 |
s[::2] | 'Pto' | Every 2nd character |
s[::-1] | 'nohtyP' | Reversed |
s[:] | 'Python' | Full copy |
Common mistakes
- Expecting the stop index to be included.
"Python"[0:3]is"Pyt", not"Pyth". - Using
len(s)as an index. The last valid index islen(s) - 1. Uses[-1]instead. - Getting an empty result with a negative step. With
step < 0,startmust be to the right ofstop. - Trying to change a string through a slice. Build a new string instead.
- Forgetting that indexes start at 0. The third character is
s[2].
Practice exercises
Exercise 1: Print the first, middle, and last character of an odd-length string.
Output:
P h 3
Exercise 2: Swap the first two and last two characters of a string.
Output:
fgcdeab
Exercise 3: Print every character at an even index, then every character at an odd index.
Output:
Pntv Yaie
Exercise 4: Check whether the second half of a string is the reverse of the first half.
Output:
True False
Summary
- Indexes start at
0from the left and-1from the right.s[i]returns one character. - An index outside the string raises
IndexError, but slices outside the string just return what’s available. s[start:stop:step]includesstart, excludesstop, and moves bystep.- Leave out
startorstopto slice from the beginning or to the end.s[:]makes a copy. - Negative indexes count from the end:
s[-3:]gives the last 3 characters. - A negative step slices backward.
s[::-1]reverses a string. - Slices create new strings. Use
slice()objects to name and reuse slices.
Also, See:

Leave a Reply