Indexing and Slicing in Python

Fundamentals: Indexing and Slicing in Python

Indexing

We have just learned about strings. In Python, strings are sequences of characters. Each character in a string has an index, which is its position in the string. Indexing allows us to access individual characters in a string using their index.

There are two key ideas to remember about indexing in Python:

  • Indices start at 0. The first character in a string has an index of 0, the second character has an index of 1, and so on.
  • To access a character in a string, we use square brackets [] and the index of the character we want to access.
text = "Hello, World!"
print(text[0])  # First character
H
print(text[7])  # Eighth character
W
print(text[-1])  # Last character
!
print(text[-2])  # Second to last character
d

Variables can be used as indices, but only if they are integers. For example:

index = 4
print(text[index])  # Fifth character
o
index = 5.0
# This will raise an error because the index is not an integer
# print(text[index])

Slicing

Slicing allows us to extract a portion of a sequence by specifying a start index and an end index. The syntax for slicing is sequence[start:end], where start is the index of the first element we want to include, and end is the index of the element we want to exclude.

text = "Hello, World!"

# We want to extract the word "Hello"
print(text[0:5])  # Start at index 0, end at index 5 (exclusive)
Hello
# We want to extract the word "World"
print(text[7:12])  # Start at index 7, end at index 12 (exclusive)
World

We can also use negative indices for slicing:

# We want to extract the word "World" using negative indices
print(text[-6:-1])  # Start at index -6, end at index -1 (exclusive)
World

In the sequence[start:end] syntax, we can leave any of the start or end values blank.

  • If we leave start blank, it defaults to the beginning of the sequence.
  • If we leave end blank, it defaults to the end of the sequence.

For instance, the following slices are equivalent:

print(text[:5])  # Start at the beginning, end at index 5 (exclusive)
print(text[0:5])  # Start at index 0, end at index 5 (exclusive)
Hello
Hello

Using a Step in Slicing

Additionally, we can include a step in our slice, which allows us to skip elements. The syntax for this is sequence[start:end:step].

# We want to extract every second character from the string
print(text[0:12:2])  # Start at index 0, end at index 12 (exclusive), step by 2
Hlo ol

In the sequence[start:end:step] syntax, we can leave any of the start, end, or step values blank.

  • If we leave start blank, it defaults to the beginning of the sequence.
  • If we leave end blank, it defaults to the end of the sequence.
  • If we leave step blank, it defaults to 1.

For instance, the following slices are equivalent:

print(text[:12:2])  # Start at the beginning, end at index 12 (exclusive), step by 2
Hlo ol
print(text[0::2])  # Start at index 0, go to the end, step by 2
Hlo ol!

And, in case you were wondering, yes: steps can be negative! This allows us to reverse a string or sequence.

# We want to reverse the string
print(text[::-1])  # Start at the end, go to the beginning, step by -1
!dlroW ,olleH

String Methods that Use Indexing

In the previous page, we learned about some string methods that use indexing. For example, the find() method returns the index of the first occurrence of a substring in a string. Go back and practice using these methods to reinforce your understanding of indexing.


Looking Ahead

Indexing and slicing not only apply to strings but also to other sequences in Python, such as lists and tuples. In the next module, we will explore how to use indexing and slicing with these data structures.