Concepts / Indexing Strings to Access Individual Characters

Indexing Strings to Access Individual Characters

len() is a built-in Python function that counts and returns the total number of characters in a string.

  • Programming

A First Length Prediction

A string is made from individual characters arranged in order. Python's built-in len() function measures how many characters the string contains and returns that total as an integer. Before reading the result, predict the length of the string banana.

What do you think happens?

What value should len('banana') produce?

  • 5

  • 6

  • 7

Reveal answer
Answer:

6

The string contains the six characters b, a, n, a, n, and a. len() returns the total number of characters.

Characters and the Returned Total

bcharacter 16len('banana')acharacter 2ncharacter 3acharacter 4ncharacter 5acharacter 6countedcountedcountedcountedcountedcounted
How do the individual characters relate to the total returned by len()?

len() counts every character in the string and returns the total. In banana, each letter contributes one character, so the result is 6. The result is an integer, which is a number, rather than another string.

python
Output
6

Counting Spaces and Symbols

The letters in a word are not the only characters that count. len() also includes spaces, punctuation, digits, and special symbols. When you predict a string's length, inspect every character instead of counting letters alone.

Hiletters7total charactersspaceone character!punctuation42digits2 counted1 counted1 counted2 counted
Which characters are included when len() measures a string containing letters, a space, punctuation, and digits?
Worked example

Measuring a Mixed String

Determine the result of len('Hi! 42').

  1. 1
    Count the letters

    H and i contribute 2 characters.

  2. 2
    Count the punctuation

    The exclamation mark contributes 1 character.

  3. 3
    Count the space

    The space between the punctuation mark and the digits contributes 1 character.

  4. 4
    Count the digits

    4 and 2 contribute 2 characters.

Result

The total is 2 + 1 + 1 + 2 = 6 characters.

python
Output
6

Length Changes with Added Characters

catlen = 3cat!len = 4cat !len = 5add !add space
How does the length change when a letter, a space, or punctuation is added?

Adding any character increases the count by one for that added character. A punctuation mark counts just as a letter does, and a space also counts. Removing one of those characters decreases the total by one.

What do you think happens?

What should len('cat !') produce?

  • 3

  • 4

  • 5

Reveal answer
Answer:

5

The string contains c, a, t, a space, and !. The space and punctuation are both included.

Practical String Measurements

String length is useful whenever a program must measure text. A program can use len() to validate user input, such as checking whether a password is long enough. It can also help truncate text so a message fits within a limited display area, or support processing a string character by character.

python
Output
9

Mistakes in Length Predictions

  • Common mistake

    Counting only letters

    What it looks like

    Predicting a smaller length for a string that contains spaces or punctuation

    Why it fails

    len() counts all characters, including spaces, punctuation, digits, and special symbols.

    Do this instead

    Inspect the complete string and count every character.

  • Common mistake

    Forgetting the parentheses

    What it looks like

    Writing len instead of calling len() with a string

    Why it fails

    The function call uses parentheses to receive the string being measured.

    Do this instead
    Write the string inside len(), as in len('banana').
  • Common mistake

    Treating the result as text

    What it looks like

    Assuming that len('banana') returns the string '6'

    Why it fails

    len() returns an integer.

    Do this instead

    Use the result as a number when making measurements or comparisons.

Practice with Character Counts

EASY

Predict the result of len('Room 7!'). Count the letters, the space, the digit, and the punctuation before checking your answer.

Hints
  • Count the four letters in Room.
  • Include the space after Room.
  • Count the digit 7 and the exclamation mark.
Output
7

The count is 7: four letters, one space, one digit, and one punctuation mark. This same inspection process works when measuring names, messages, passwords, or other strings.

Key Takeaways

  1. 1
    len() is a built-in Python function that returns the total number of characters in a string.
  2. 2
    Letters, spaces, punctuation, digits, and special symbols all contribute to the length.
  3. 3
    Use parentheses when calling len(), and remember that its result is an integer.
  4. 4
    Accurate length measurement supports input validation, text display limits, and character-by-character processing.

At a glance

  • len() counts every character in a string and returns an integer.
  • Spaces and punctuation are included, not ignored.
  • To predict a result, inspect and count each character type in the complete string.
  • String length measurements can support validation, display limits, and character-by-character processing.

Related concepts

More concepts starting with I