Home Course Roadmap
Lesson 16 · Strings

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.

35–45 minutes
Beginner
Python Foundations
0%

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.
01

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.

Think of a string like a row of boxes.

Each character occupies one box, and the index tells Python which box you want to access.

Example
word = "Python"

print(word[0])
print(word[1])
print(word[2])
Output
P
y
t
02

Zero-Based Indexing

Python uses zero-based indexing. This means the first character has index 0, not 1.

Python
word = "Python"

# Index:  0  1  2  3  4  5
# Char:   P  y  t  h  o  n

Therefore:

Accessing characters
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
Remember: The first position is always index 0.
03

Positive Indexing

Positive indexing starts from the beginning of the string and moves from left to right.

Example
language = "Python"

print(language[0])
print(language[3])
print(language[5])
Output
P
h
n

The index increases by one as you move toward the right.

04

Negative Indexing

Python also allows you to access characters from the end of a string using negative indexes.

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.

Example
word = "Python"

print(word[-1])
print(word[-2])
print(word[-6])
Output
n
o
P
Useful shortcut:

Use text[-1] when you need the last character without knowing the string's length.

05

Accessing Individual Characters

You can use an index directly inside square brackets to access a character.

Character access
name = "Adnan"

print(name[0])
print(name[2])
print(name[-1])
Output
A
n
n

Indexing returns exactly one character.

06

IndexError

What happens if you try to access an index that does not exist?

Invalid index
word = "Python"

print(word[10])
Result: IndexError

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.

Safer indexing idea
word = "Python"

index = 4

if index < len(word):
    print(word[index])
07

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.

Basic idea
text[start:stop]

The start position is included, while the stop position is excluded.

08

Basic Slice Syntax

Let's extract part of the word Python.

Example
word = "Python"

print(word[0:2])
print(word[2:6])
Output
Py
thon

Notice that index 2 is included in the second slice, but index 6 is not.

09

Start & Stop Rules

Python slicing follows a simple rule:

Start is included. Stop is excluded.

This is one of the most important rules to remember when working with slices.

Example
word = "Python"

print(word[1:4])
Output
yth

Indexes 1, 2, and 3 are included. Index 4 is excluded.

10

Omitting the Start Index

If you leave out the start index, Python starts from the beginning of the string.

Example
word = "Python"

print(word[:4])
Output
Pyth

This is equivalent to starting from index 0.

Equivalent expressions
word[:4]

word[0:4]
11

Omitting the Stop Index

If you leave out the stop index, Python continues until the end of the string.

Example
word = "Python"

print(word[2:])
Output
thon

This means "start at index 2 and continue to the end."

12

The Step Value

Slicing can also accept a third value called step.

Slice syntax
text[start:stop:step]

The step determines how many positions Python moves between selected characters.

Example
word = "Python"

print(word[::2])
Output
Pto

Python selects every second character.

13

Negative Step & Reversing Strings

A negative step moves through the string from right to left.

One of the most useful Python slicing techniques is:

Reverse a string
word = "Python"

reversed_word = word[::-1]

print(reversed_word)
Output
nohtyP
Remember:

[::-1] is a common Python pattern for creating a reversed version of a sequence.

14

Common Slicing Patterns

Here are several patterns you will use frequently.

Useful patterns
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
Output
Pyt
hon
Python
Pto
Ph
nohtyP
15

Strings Are Immutable

Python strings are immutable. This means that individual characters cannot be changed directly.

Invalid operation
word = "Python"

word[0] = "J"
TypeError

You cannot assign a new character directly to an existing string position.

Instead, create a new string.

Correct approach
word = "Python"

word = "J" + word[1:]

print(word)
Output
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.

Open Practice Lab