Skip to content
Theory · Strings · lesson 1 of 10

Strings and indices

What you'll learn

What you'll learn:

  • take a character of a string by its number—from the start and from the end;
  • cut a piece out of a string with bounds and a step;
  • explain why a string cannot be changed, and get new strings with methods;
  • build text out of values with f-strings.
Five tiles spelling lemon with the numbers 0 1 2 3 4 under them, the first tile with a tag word[0] lifted

Picture a rack of tiles from a word game. Five tiles with the letters l, e, m, o, n stand on it, spelling the word lemon. Under each tile is its number: 0, 1, 2, 3, 4.

To take the first tile, you call out its number, and the tile rises above the rack. The lifted tile carries a tag, word[0]: that is what Python calls this tile.

A string in Python works the same way. It stores characters in order, like tiles on a rack, and each character has a number. A program often needs one character: the first letter of a name, the last digit of an order code. The number of the character you need goes in square brackets after the string:

Python
word = "lemon"
print(len(word))  # 5
print(word[0], word[1], word[4])  # l e n
print(word[-1], word[-5])  # n l
print("mon" in word, "Mon" in word)  # True False

Let's go through the example line by line:

  1. word = "lemon" binds the name word to a string of five characters.
  2. len(word) counts the tiles on the rack: there are 5.
  3. word[0], word[1] and word[4] are the first, second and last letters: l e n.
  4. word[-1] and word[-5] count from the right edge: the last letter n and the first, l.
  5. "mon" in word checks whether the tiles m, o, n stand on the rack in a row, and "Mon" in word looks for a capital M.

Index

The number of a character in a string is called its index. Counting starts at zero: word[0] is the first character, word[1] is the second.

People count things from one, so this feels odd at first. This rule helps: the index tells how many tiles stand to the left. No tile stands left of the first one, so its index is 0.

The len function returns the length of a string—the number of characters in it. So the last index is one less than the length: in the five-character string "lemon" it is 4.

What else to know about an index:

  • word[0] returns a string of one character, of type str: you can print it, compare it or join it with another string;
  • a space is a character too, and len("a b") is 3;
  • the empty string "" has length 0 and no indices at all.

A string is a sequence: its values stand in order, and each one can be reached by its index. The loop for ch in word goes through the characters in the same order, from index 0 to the last one.

Here the rack analogy ends with one caveat. A tile on a rack can be swapped for another, but a character in a string cannot. A string can be read by index, but not rewritten.

Negative indices

You often need a character from the end: the last digit of a number, the last letter of a word. Counting the length every time is inconvenient, so characters have a second numbering—from right to left.

A negative index counts characters from the end: word[-1] is the last character, word[-2] is the one before it. Python replaces a negative index i with len(word) + i. So word[-1] is the same as word[len(word) - 1]. The leftmost negative index is -len(word): in a five-letter word, word[-5] is the first letter, l.

This is how you take the last character without counting the length:

Python
code = "A-1047"
print(code[-1], code[-4])  # 7 1
print(code[len(code) - 1])  # 7

Let's go through it line by line:

  1. code[-1] is the last character, 7, and code[-4] is the fourth from the end, 1.
  2. code[len(code) - 1] gives the same 7, but it is longer and easier to get wrong.

Interviewers like to ask this: "What do s[-1] and s[-len(s)] return?" The answer is the last and the first characters of the string.

The in operator

The expression a in s checks whether the string a occurs inside s as a whole: character after character, in a row. The result is True or False.

The in operator checks a single character and a whole word alike:

  • "e" in word and "mon" in word are both True;
  • "mn" in word is False: both letters are there, but not in a row;
  • "Mon" in word is False too: letter case matters;
  • "x" not in word is True: not in gives the opposite answer.

Common mistake

The last character is taken by an index equal to the length:

Python
word = "lemon"
print(word[len(word)])  # ✗
TEXT
IndexError: string index out of range

The indices of a five-character string run from 0 to 4, and it has no index 5. Python stops the program with an IndexError. A negative index left of the first character, such as word[-6], gives the same error. The last character is word[len(word) - 1], or shorter, word[-1].

Key points

  • An index is the number of a character in a string; counting starts at zero, and the last index is len(s) - 1.
  • A negative index counts from the end: s[-1] is the last character, s[-len(s)] the first.
  • An index outside the string raises an IndexError.
  • a in s checks whether a stands inside s in a row, and case matters.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way