Access Individual Characters and Substrings in Python

Learn how to retrieve individual characters and ranges of characters from Python strings with zero-based indexing, slicing, and negative indexes.

Strings are sequences of characters

A string is an ordered sequence of text characters enclosed in quotes. A character is one item in that sequence, such as a letter, digit, space, or punctuation mark.

Python gives every character a numeric position called an index. The position includes every character: letters, digits, spaces, and punctuation.

message = "Hello world!"

In this string, the space between Hello and world! occupies a position just like the letters. The exclamation mark also has its own position.

Access one character with indexing

Indexing means using square brackets and one index to retrieve a single item from a sequence. The general form is:

string_variable[index]

Indexing a string returns a one-character string.

message = "Hello world!"
print(message[0])

Output:

H

Zero-based indexing

Python uses zero-based indexing. This means the first character has index 0, not index 1. Positions increase from left to right.

Character Positions in a Sample String

Character | Positive index | Negative index

H | 0 | -12

e | 1 | -11

l | 2 | -10

l | 3 | -9

o | 4 | -8

space | 5 | -7

w | 6 | -6

o | 7 | -5

r | 8 | -4

l | 9 | -3

d | 10 | -2

! | 11 | -1

For example, the character after the space is at index 6:

message = "Hello world!"
print(message[6])

Output:

w

The space is at index 5, so it must be counted before the w.

Extract a range with slicing

A slice is a portion of a sequence selected with a starting and ending boundary. Use this notation:

string_variable[start:end]

The start index is included, but the end index is excluded. This is called an exclusive end.

message = "Hello world!"
print(message[0:3])

Output:

Hel

The slice begins at index 0 and stops before index 3. It therefore includes indexes 0, 1, and 2.

Indexing and slicing have different results:

  • message[0] retrieves one character: H.
  • message[0:3] retrieves several characters: Hel.

Common indexing and slicing patterns

Python String Indexing and Slicing Patterns

Expression pattern | Meaning | Example | Result

string[index] | One character at index | message[0] | H

string[start:end] | Start included, end excluded | message[0:3] | Hel

string[:end] | From the beginning through before end | message[:5] | Hello

string[start:] | From start through the final character | message[5:] | world!

string[-1] | Final character | message[-1] | !

Omit slice boundaries

You can leave out one boundary when the desired range reaches the beginning or end of the string.

Slice from the beginning

[:end] starts at the beginning of the string, index 0, and stops before end.

message = "Hello world!"
print(message[:5])

Output:

Hello

Slice through the end

[start:] begins at start and continues through the final character.

message = "Hello world!"
print(message[5:])

Output:

 world!

The result begins with a space because the space is at index 5.

Use negative indexes

A negative index counts backward from the right end of the string. The index -1 identifies the last character, -2 identifies the character before it, and so on.

message = "Hello world!"
print(message[-1])

Output:

!

Negative indexing is useful when you need a character near the end without first calculating its positive index.

Try indexing and slicing in the Python shell

You can run these assignments and expressions in an interactive Python shell. Expressions entered at the prompt display their results.

message = "Hello world!"
message[0]
message[6]
message[0:3]
message[:5]
message[5:]
message[-1]

The results are, in order, 'H', 'w', 'Hel', 'Hello', ' world!', and '!'.

Reading versus changing strings

The examples in this lesson read values from a string. Python strings are immutable, which means they cannot be changed in place after creation.

word = "cat"
word[0] = "b"

This raises a TypeError. Bracket notation can retrieve a character, but it cannot assign a replacement directly to a string position. To produce bat, create a new string instead:

word = "cat"
word = "b" + word[1:]
print(word)

Output:

bat

Index and slice boundary errors

A single-character index must exist. If an index is outside the available positions, Python raises IndexError.

word = "cat"
print(word[3])

The valid positive indexes are 0, 1, and 2, so index 3 does not exist.

You can check a string's length with len():

word = "cat"
print(len(word))

Output:

3

Slices behave differently. A slice can extend beyond a string boundary and returns the available portion without raising an error.

word = "cat"
# word[3] raises IndexError
print(word[1:10])

Output:

at

The slice starts at index 1 and returns the characters available through the end of the string.

Troubleshooting indexing and slicing

  • The character is one position off: Counting probably began at 1. Remember that the first character is at index 0.
  • A slice contains fewer characters than expected: The end boundary is exclusive. Increase the ending index when the desired character should be included.
  • A character after a word has an unexpected index: Count the space and any punctuation. They occupy positions too.
  • Python raises IndexError: The requested positive or negative index is outside the string. Use a valid index, check len(), or use a slice when appropriate.
  • Bracket assignment fails: Strings are immutable. Build a new string using slices and replacement text.

Summary

  • A string is an ordered sequence of characters, including spaces and punctuation.
  • Use string[index] to retrieve one character.
  • Python uses zero-based indexing, so the first character is at index 0.
  • Use string[start:end] to retrieve a range; the end boundary is excluded.
  • Use [:end] for a prefix and [start:] for a suffix.
  • Use negative indexes such as -1 to count from the end.
  • Invalid individual indexes raise IndexError, while oversized slices safely return available characters.
  • Strings are immutable, so indexing cannot be used to change a character in place.

Continue practicing with individual character and substring access in Python.