Skip to content

Python String Indexing and Slicing

Updated 6 min read Aimerscrew

Sometimes you don't need a whole string, just a piece of it: the first letter of a name, the last four digits of a card, the year in a date. Python string slicing and indexing let you pull out exactly the characters you want. In this lesson, you'll learn positive and negative indexes, the [start:stop:step] syntax, and the one-line trick for reversing a string.

🌍 Real-life example

When you pay online, the receipt often shows your card as **** 4821. The shop's program takes the full card number and keeps only the last four characters. Airlines do something similar when they print the first three letters of your destination city on a baggage tag. Both are string slicing.

What is a string index in Python?

As you saw in Python strings, a string is a row of characters. Each character has a position number called an index. Python counts from 0, so the first character is at index 0, the second at index 1, and so on.

To read one character, put its index in square brackets after the string:

word = "Python"
print(word[0])
print(word[1])
print(word[5])
P
y
n

The result is itself a string, just one character long.

Negative indexes: counting from the end

Python also numbers characters from the right, using negative indexes. The last character is -1, the one before it is -2, and so on. This is perfect when you care about the end of a string and don't know how long it is.

word = "Python"
print(word[-1])
print(word[-2])
print(word[-6])
n
o
P

Every character has two indexes, one positive and one negative. In "Python", the letter h is both word[3] and word[-3].

What happens if the index doesn't exist?

Asking for a position past the end raises an IndexError:

word = "Python"
print(word[6])
IndexError: string index out of range

A 6-character string has indexes 0 to 5 (or -1 to -6). The highest valid positive index is always len(word) - 1.

What is string slicing in Python?

A slice is a piece of a string taken from one position up to another. The syntax is text[start:stop]:

  • start is the index of the first character you want. It is included.
  • stop is the index where the slice ends. It is not included.
word = "Python"
print(word[0:2])
print(word[2:5])
print(word[1:4])
Py
tho
yth

A handy way to remember it: the slice [start:stop] gives you stop - start characters. word[2:5] gives 3 characters.

Animation: the string Python in six boxes with indexes 0 to 5 above and -6 to -1 below; a highlight selects word[0:2] giving Py, then word[2:5] giving tho, then slides right to word[-3:] giving hon
A slice is a window over the characters. The start index is inside the window; the stop index is just outside it.

Omitting the start or the stop

You can leave out either number. A missing start means "from the beginning", and a missing stop means "to the end":

word = "Python"
print(word[:3])    # first 3 characters
print(word[3:])    # everything from index 3 on
print(word[:])     # a copy of the whole string
Pyt
hon
Python

Omitting combines nicely with negative indexes:

card = "4539876512344821"
print(card[-4:])   # last 4 characters
print(card[:-4])   # everything except the last 4
4821
453987651234

💡 Tip

text[:n] and text[n:] always fit together perfectly: text[:n] + text[n:] gives back the whole string, with no character lost or repeated. That's one reason the stop index is not included.

Using a step: [start:stop:step]

A third number, the step, tells Python how far to jump between characters. The default step is 1 (every character). A step of 2 takes every second character:

letters = "abcdefghij"
print(letters[::2])
print(letters[1::2])
print(letters[0:7:3])
acegi
bdfhj
adg

How do you reverse a string in Python?

A negative step walks backward through the string. With start and stop left out, [::-1] walks from the last character to the first, which reverses the string:

print("stressed"[::-1])
print("Python"[::-1])

word = "level"
print(word == word[::-1])
desserts
nohtyP
True

The last check is a classic: a word that reads the same backward is a palindrome.

Slicing never raises an error

Here's an important difference. Indexing a position that doesn't exist raises IndexError, but a slice that goes past the end just stops at the edge. If nothing fits, you get an empty string.

code = "AB12"
print(code[2:100])
print(code[10:20])
print(len(code[10:20]))
12

0

The second line printed an empty string, which shows up as a blank line. This forgiving behavior makes slices safe for text of unknown length: name[:10] works on a 3-letter name and on a 30-letter name.

ExpressionOn "Python"Result
text[i]word[0]'P'
text[-i]word[-1]'n'
text[start:stop]word[1:4]'yth'
text[:stop]word[:2]'Py'
text[start:]word[-3:]'hon'
text[::step]word[::2]'Pto'
text[::-1]word[::-1]'nohtyP'
Index out of rangeword[9]IndexError
Slice out of rangeword[9:]'' (empty)

Real-world examples of string slicing

Extracting an area code

phone = "212-555-0187"
area_code = phone[:3]
line_number = phone[-4:]
print("Area code:", area_code)
print("Line:", line_number)
Area code: 212
Line: 0187

Getting a file extension

filename = "holiday_photo.jpeg"
print(filename[-4:])
print(filename[-5:] == ".jpeg")
jpeg
True

This works when you know how long the extension is. For any length, the .split() and .endswith() tools in Python string methods are a better fit.

Building initials

first = "grace"
last = "hopper"
initials = first[0] + last[0]
print(initials)
print(first[0].upper() + "." + last[0].upper() + ".")
gh
G.H.

The .upper() part turns a letter into uppercase. You'll learn it, and many more, in the next lesson.

Splitting a date

date = "2026-07-14"
year = date[:4]
month = date[5:7]
day = date[8:]
print(day, month, year)
14 07 2026

Common mistakes with string slicing

1. Expecting the stop character to be included

word = "Python"
print(word[0:3])
Pyt

Beginners often expect Pyth. Remember: the stop index is where the slice stops, so it's left out.

2. Using len() as an index

word = "Python"
print(word[len(word)])
IndexError: string index out of range

The last character is at len(word) - 1, or more simply word[-1].

3. Trying to change a character with an index

word = "Python"
word[0] = "J"
TypeError: 'str' object does not support item assignment

Strings are immutable. Build a new string with a slice instead: word = "J" + word[1:] gives Jython.

4. Mixing up a reversed slice's start and stop

With a negative step, start must be to the right of stop. "Python"[1:4:-1] gives an empty string, while "Python"[4:1:-1] gives oht. When in doubt, slice forward first and reverse with [::-1] afterward.

Practice exercise

🏋️ Try it yourself

You have a flight code: flight = "BA0117-LHR-JFK". Use slicing to print the airline (BA), the flight number (0117), the departure airport (LHR) and the arrival airport (JFK). Then print the whole code reversed.

Show solution
flight = "BA0117-LHR-JFK"

print("Airline:", flight[:2])
print("Number:", flight[2:6])
print("From:", flight[7:10])
print("To:", flight[-3:])
print(flight[::-1])
Airline: BA
Number: 0117
From: LHR
To: JFK
KFJ-RHL-7110AB

What's next?

Now you can pick out any character or piece of a string. Next, discover the built-in tools that clean, search and transform text in Python string methods.

Key takeaways

  • Indexes start at 0 from the left and at -1 from the right, so text[0] is the first character and text[-1] the last.
  • text[start:stop] returns a slice: start is included, stop is not.
  • Leave out start to begin at the start of the string, and leave out stop to go to the end.
  • A third number sets the step: text[::2] takes every second character and text[::-1] reverses the string.
  • An index out of range raises IndexError, but an out-of-range slice simply returns what fits, or an empty string.
  • Slicing always returns a new string; the original never changes.

Frequently asked questions

How do you slice a string in Python?

Use square brackets with a colon: text[start:stop]. For example, "Python"[1:4] returns "yth". The character at start is included and the character at stop is not.

How do you get the last character of a string in Python?

Use the negative index -1: text[-1]. For the last n characters, use a slice such as text[-4:].

How do you reverse a string in Python?

Use a slice with a step of -1: text[::-1]. For example, "stressed"[::-1] returns "desserts".

Why is the stop index not included in a Python slice?

It makes slices easy to combine and measure: text[:n] + text[n:] always equals text, and text[a:b] always has b - a characters.

Does string slicing raise IndexError in Python?

No. Only single-character indexing like text[10] raises IndexError. A slice like text[10:20] just returns an empty string if those positions do not exist.