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

countedcountedcountedcountedcountedcountedbcharacter 16len('banana')acharacter 2ncharacter 3acharacter 4ncharacter 5acharacter 6
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.

2 counted1 counted1 counted2 countedHiletters7total charactersspaceone character!punctuation42digits
Which characters are included when len() measures a string containing letters, a space, punctuation, and digits?

Measuring a Mixed String

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

Count the letters: H and i contribute 2 characters.

Count the punctuation: The exclamation mark contributes 1 character.

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

Count the digits: 4 and 2 contribute 2 characters.

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

python
Output
6

Length Changes with Added Characters

add !add spacecatlen = 3cat!len = 4cat !len = 5
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

  • Counting only letters

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

    Fix: Inspect the complete string and count every character.

  • Forgetting the parentheses

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

    Fix: Write the string inside len(), as in len('banana').

  • Treating the result as text

    len() returns an integer.

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

Key Takeaways

  • 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.