String Slicing and Substrings
len() is a built-in Python function that counts and returns the total number of characters in a string.
A Quick Prediction
Suppose a program needs to know how long a piece of text is. Before seeing the result, predict the length of the word 'banana'. Count each character carefully. The answer is six because the word contains six characters.
What do you think happens?
What value does len() return for the string 'banana'?
Reveal answer
Answer: 6
The characters are b, a, n, a, n, and a. Each character contributes one to the total.
Counting Every Character
len() is a built-in Python function that counts and returns the total number of characters in a string. The returned result is an integer, not a string.
The word character is broader than letter. Letters count, but so do spaces, punctuation marks, digits, and special symbols. A space between two words contributes to the length just like a letter does. An underscore also counts as a character.
Spaces and Symbols
Predicting a Mixed String Length
Predict the length of a string containing letters, a space, punctuation, and digits.
Identify the letters: Count every letter in the text.
Add the space: The space is a character, even though it may not be visually prominent.
Add punctuation and digits: Punctuation marks and digits also contribute to the total.
Check the complete count: The predicted result is the count of all characters, not only the letters.
A correct prediction includes letters, spaces, punctuation, digits, and other symbols present in the string.
When predicting a result, make the invisible visible by separating the characters mentally. For a phrase, count the words' letters and then add the spaces. For text containing punctuation or numbers, add those marks as well. This prevents the common mistake of counting only alphabetic characters.
Measuring User Input
String length is useful when a program needs to measure text. One practical use is validating user input, such as checking whether a password is long enough. Another is checking whether a username meets a length requirement. The source example describes a username whose length is 10; because 10 is greater than or equal to 5, the program reports that the username is valid. The underscore in that username is included in the count.
The general reasoning is simple: obtain the string's character count, compare that count with the required limit, and use the result to decide whether the text meets the rule. The same measurement idea can help truncate text for display or loop through a string character by character.
Mistakes in Predictions
Counting only letters
len() counts all characters in the string, not just alphabetic characters.
Fix:
Count every visible character and include spaces and symbols in the prediction.Forgetting the parentheses
The function call requires parentheses.
Fix:
Use len() when calling the function.Treating the result as text
len() returns an integer.
Fix:
Handle the result as a number when using it in a length comparison.
Before checking a result, count the characters yourself. Read the string from beginning to end and account for every letter, space, punctuation mark, digit, and special symbol. Then compare your prediction with the integer returned by len().
Practice and Recall
Predict the result of len() for three strings: one containing only letters, one containing letters separated by a space, and one containing letters together with punctuation and digits. For each prediction, explain which characters you counted.
Hints
- Move from left to right through each string.
- Include the space even though it is visually empty.
- Count punctuation and digits just as you count letters.
- len() counts and returns the total number of characters in a string. Every character counts, including letters, spaces, punctuation, digits, and special symbols. The function must be called with parentheses, and its result is an integer. Accurate length predictions come from counting the entire string rather than only its letters. In practical programs, that count can support input validation, display limits, and character-by-character processing.
Key Takeaways
- len() returns the total number of characters in a string.
- Spaces, punctuation, digits, and special symbols are included in the count.
- Use parentheses when calling len(), and remember that the result is an integer.
- Counting every character helps you predict results and apply length checks to practical input.