Sub topic 2
Reading one character with a zero-based index
This chapter assumes you can run Python code in a REPL or script, assign a string literal to a variable, and use print() and len(). You are building on the idea that a string is an ordered sequence of characters. Here you learn how to pick out one character from it.
The index is a position number
An index is an integer in square brackets that names a position in a sequence. Python starts counting at 0, so the first character is at index 0, the second at index 1, and so on.
s = "Python"
print(s[0])
print(s[5])This prints:
P
nThe string "Python" has six characters, so its positions are 0 through 5.
Why counting starts at zero
You can think of the index as an offset: how many steps you move from the start of the string. The first character is zero steps away, so its index is 0. The character at index 3 is three steps from the start.
This has a practical consequence. For a string of length n, the valid non-negative indexes run from 0 to n - 1. The last character is therefore at len(s) - 1, not at len(s).
s = "Python"
last_index = len(s) - 1
print(last_index)
print(s[last_index])This prints 5 and then n.
What an index returns
Indexing a string returns a new string of length 1. Python has no separate character type. A single character is just a short string.
s = "Python"
first = s[0]
print(first)
print(type(first))
print(len(first))The output is:
P
<class 'str'>
1Because the result is an ordinary string, you can use it like any other. You can compare it, concatenate it, or call methods on it.
word = "python"
print(word[0].upper() + word[1:])This prints Python. The word[1:] part is a slice, which a later chapter covers. For now, notice that word[0] gave you something you could call .upper() on.
Using a variable as an index
The index can be any expression that produces an integer. A variable, arithmetic, or a call to len() all work.
s = "Python"
i = 2
print(s[i])
print(s[i + 1])
print(s[len(s) // 2])This prints t, h, and h. The last line computes 6 // 2, which is 3, so it reads index 3.
A float is not allowed, even if it holds a whole number. s[2.0] raises a TypeError, because indexes must be integers. If a calculation produces a float, convert it with int() or use // for integer division.
Walking over positions
When you need both the position and the character, enumerate() gives you the pair without manual counting. Its numbering starts at 0 by default, matching the string's indexes.
s = "Python"
for i, ch in enumerate(s):
print(i, ch)This prints each index next to its character, from 0 P to 5 n. Prefer this over for i in range(len(s)) followed by s[i] when you need both values. It is shorter and has fewer chances for an off-by-one mistake.
Common mistakes
- Treating the first character as index 1.
s[1]is the second character. - Using
s[len(s)]to get the last character. That position is one past the end, so it is not valid. - Confusing the index with the count. A string with 6 characters has a largest index of 5.
The next chapter shows how negative indexes count from the end. A later one covers what happens when an index points outside the string.
Exercise
Given s = "Python", what does s[3] return, and what is the index of the character 'o'?
Show answer
s[3] returns 'h', because the characters at indexes 0 to 5 are P, y, t, h, o, n. The character 'o' is at index 4.
Exercise
A string text has 10 characters. What is the largest valid non-negative index, and which expression gives it without hard-coding the number?
Show answer
The largest valid index is 9. The expression len(text) - 1 gives it, and text[len(text) - 1] reads the last character.