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
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').
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.
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
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
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
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
- len() is a built-in Python function that returns the total number of characters in a string.
- Letters, spaces, punctuation, digits, and special symbols all contribute to the length.
- Use parentheses when calling len(), and remember that its result is an integer.
- 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.