Python String Indexing & Slicing
Learn how to access individual characters, extract parts of strings, reverse text, and work with Python's powerful indexing and slicing syntax.
By the end of this lesson, you will be able to:
- Understand how Python indexes strings.
- Use positive and negative indexes.
- Access individual characters safely.
- Understand the difference between indexing and slicing.
- Extract portions of a string using slice notation.
- Use start, stop, and step values.
- Reverse strings using slicing.
- Avoid common indexing and slicing mistakes.
What Is String Indexing?
Indexing means accessing a specific character from a string using its position.
Python treats a string as a sequence of characters. Each character has a position called an index.
Each character occupies one box, and the index tells Python which box you want to access.
word = "Python"
print(word[0])
print(word[1])
print(word[2])
P y t
Zero-Based Indexing
Python uses zero-based indexing.
This means the first character has index 0,
not 1.
word = "Python"
# Index: 0 1 2 3 4 5
# Char: P y t h o n
Therefore:
word = "Python"
print(word[0]) # P
print(word[1]) # y
print(word[2]) # t
print(word[3]) # h
print(word[4]) # o
print(word[5]) # n
0.
Positive Indexing
Positive indexing starts from the beginning of the string and moves from left to right.
language = "Python"
print(language[0])
print(language[3])
print(language[5])
P h n
The index increases by one as you move toward the right.
Negative Indexing
Python also allows you to access characters from the end of a string using negative indexes.
word = "Python"
# Positive: 0 1 2 3 4 5
# Character: P y t h o n
# Negative: -6 -5 -4 -3 -2 -1
The last character is always at index -1.
word = "Python"
print(word[-1])
print(word[-2])
print(word[-6])
n o P
Use text[-1] when you need the last
character without knowing the string's length.
Accessing Individual Characters
You can use an index directly inside square brackets to access a character.
name = "Adnan"
print(name[0])
print(name[2])
print(name[-1])
A n n
Indexing returns exactly one character.
IndexError
What happens if you try to access an index that does not exist?
word = "Python"
print(word[10])
The string has indexes from 0 to
5, so index 10 does not exist.
You can use len() to determine the number of
characters before accessing a specific position.
word = "Python"
index = 4
if index < len(word):
print(word[index])
What Is String Slicing?
Slicing allows you to extract a portion of a string instead of accessing only one character.
Slicing uses a colon inside square brackets.
text[start:stop]
The start position is included, while the
stop position is excluded.
Basic Slice Syntax
Let's extract part of the word Python.
word = "Python"
print(word[0:2])
print(word[2:6])
Py thon
Notice that index 2 is included in the
second slice, but index 6 is not.
Start & Stop Rules
Python slicing follows a simple rule:
This is one of the most important rules to remember when working with slices.
word = "Python"
print(word[1:4])
yth
Indexes 1, 2, and 3
are included. Index 4 is excluded.
Omitting the Start Index
If you leave out the start index, Python starts from the beginning of the string.
word = "Python"
print(word[:4])
Pyth
This is equivalent to starting from index 0.
word[:4]
word[0:4]
Omitting the Stop Index
If you leave out the stop index, Python continues until the end of the string.
word = "Python"
print(word[2:])
thon
This means "start at index 2 and continue to the end."
The Step Value
Slicing can also accept a third value called step.
text[start:stop:step]
The step determines how many positions Python moves between selected characters.
word = "Python"
print(word[::2])
Pto
Python selects every second character.
Negative Step & Reversing Strings
A negative step moves through the string from right to left.
One of the most useful Python slicing techniques is:
word = "Python"
reversed_word = word[::-1]
print(reversed_word)
nohtyP
[::-1] is a common Python pattern
for creating a reversed version of a sequence.
Common Slicing Patterns
Here are several patterns you will use frequently.
word = "Python"
print(word[:3]) # First 3 characters
print(word[3:]) # From index 3 to the end
print(word[:]) # Entire string
print(word[::2]) # Every second character
print(word[::3]) # Every third character
print(word[::-1]) # Reverse the string
Pyt hon Python Pto Ph nohtyP
Strings Are Immutable
Python strings are immutable. This means that individual characters cannot be changed directly.
word = "Python"
word[0] = "J"
You cannot assign a new character directly to an existing string position.
Instead, create a new string.
word = "Python"
word = "J" + word[1:]
print(word)
Jython
You've reached the end of this lesson.
Review the examples, open the Practice Lab, and mark this lesson complete when you're ready.