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.
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?
Reveal 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
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.
6Counting 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.
Measuring a Mixed String
Determine the result of len('Hi! 42').
- 1Count the letters
H and i contribute 2 characters.
- 2Count the punctuation
The exclamation mark contributes 1 character.
- 3Count the space
The space between the punctuation mark and the digits contributes 1 character.
- 4Count the digits
4 and 2 contribute 2 characters.
The total is 2 + 1 + 1 + 2 = 6 characters.
6Length Changes with Added Characters
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?
Reveal 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.
9Mistakes in Length Predictions
Common mistake Counting only letters
What it looks likePredicting a smaller length for a string that contains spaces or punctuation
Why it failslen()counts all characters, including spaces, punctuation, digits, and special symbols.Do this insteadInspect the complete string and count every character.
Common mistake Forgetting the parentheses
What it looks likeWriting len instead of calling len() with a string
Why it failsThe function call uses parentheses to receive the string being measured.
Do this insteadWrite the string inside len(), as in len('banana').Common mistake Treating the result as text
What it looks likeAssuming that len('banana') returns the string '6'
Why it failslen()returns an integer.Do this insteadUse the result as a number when making measurements or comparisons.
Practice with Character Counts
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.
7The 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. - 2Letters, spaces, punctuation, digits, and special symbols all contribute to the length.
- 3Use parentheses when calling
len(), and remember that its result is an integer. - 4Accurate 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.