Indexing and Slicing

To effectively work with text data, you often need to extract specific characters or portions of a string. Python's indexing and slicing provide powerful, concise ways to do exactly that, letting you precisely target the data you need.

Visualizing String Indices
This chart shows the positive (0-based) index for each character in the string 'PYTHON'. The corresponding negative index, counting from the end of the string, is shown as an annotation above each bar.
Loading chart...
Key Insight: Each character has both a positive index (from the start) and a negative index (from the end), allowing flexible access.

Accessing Single Characters

Every character in a string occupies a specific position, identified by an index. Python uses zero-based positive indexing, starting from 0 for the first character. It also supports negative indexing, which counts from the end of the string, with -1 representing the last character. This dual approach offers flexibility when you need to retrieve characters from either end of a string.

pythonPositive and Negative Indexing in Action
Check Your Understanding
Which of these indices retrieves the 'o' from 'Python'?

Extracting Substrings with Slicing [start:stop]

While indexing retrieves a single character, slicing allows you to extract a contiguous sequence of characters, known as a substring. The basic syntax is [start:stop], where start is inclusive and stop is exclusive. This means the character at the start index is included, but the character at the stop index is not.

pythonBasic Slicing Examples
Try It Yourself
Slice out 'world' from 'Hello, world!' using positive indices.
python

You can omit the start or stop index for convenience. Omitting start defaults to 0, and omitting stop defaults to the end of the string. Using [:] creates a full copy of the string, which can be useful when you need to work with a mutable version of the string (e.g., converting to a list of characters) without affecting the original.

pythonSlicing with Omitted Boundaries
Check Your Understanding
What does my_string[2:2] return?

Advanced Slicing with a Step [start:stop:step]

Beyond start and stop, slicing also accepts an optional step parameter. This allows you to skip characters, taking every Nth character within the specified range. The step value determines the increment between characters in the resulting substring, providing a powerful way to extract non-contiguous sequences.

pythonSlicing with a Step
Quick Reverse

A common trick to reverse a string is my_string[::-1]. This uses a negative step to iterate backward through the entire string, effectively creating a reversed copy.

Check Your Understanding
What substring results from 'abcdefg'[1::2]?
📌 Strings are Immutable

Indexing and slicing only retrieve characters or substrings; they do not modify the original string. Any operation that appears to change a string, such as my_string = my_string[2:], actually creates a new string and reassigns the variable, leaving the original string object untouched in memory.

Key Takeaways from Indexing and Slicing
  • Positive indices start at 0 from the left; negative indices start at -1 from the right.

  • Use string[index] to access a single character at a specific position.

  • Slicing string[start:stop] extracts a substring; start is inclusive, stop is exclusive.

  • Omitting start defaults to 0, and omitting stop defaults to the string's end.

  • The optional step parameter in string[start:stop:step] determines the increment between characters.

  • A negative step (e.g., [::-1]) is a concise way to reverse a string.

  • Indexing and slicing always return new strings or characters, never modifying the original string due to Python's string immutability.

← All lessons in String Manipulation

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free