Indexing and Slicing Strings
len() is a built-in function that returns the number of items in its argument as an integer.
The Hidden Count in a String
A string may look like one value, but it contains individual characters. The len() built-in function tells you how many items are in the value it receives. For a string, each character counts as one item, including letters, spaces, digits, and punctuation.
What do you think happens?
What should len('Hello world') return? Count every character, including the space.
Reveal answer
Answer: 11
The string contains H, e, l, l, o, a space, w, o, r, l, and d. The space is an item too, so the total is 11.
Characters and Positions
The count returned by len() also tells you how far the valid positions extend. Positions start at 0 rather than 1. Therefore, a string with length 11 has valid indices from 0 through 10. The final character is always at index length minus 1.
The number returned by len() is a count of items, not the final index. For a length of 11, the final index is 10.
Counting Every Character
Finding the final valid position
The string Hello world has length 11. What is its final valid index?
Start with the length: The length is 11 because every character, including the space, is counted.
Adjust for zero-based positions: Positions begin at 0, so subtract 1 from the length to find the final valid index.
Calculate: 11 minus 1 equals 10.
The final valid index is 10.
Beyond Strings
len() is not limited to strings. It works with collection types such as lists, tuples, dictionaries, and sets. In each case, len() returns the number of items, but the meaning of item depends on the data type. For strings, the items are characters; for other collection types, the collection defines what counts as one item.
| Collection type | What len() counts |
|---|---|
| String | Characters, including spaces and punctuation |
| List | Items in the list |
| Tuple | Items in the tuple |
| Dictionary | Items in the dictionary |
| Set | Items in the set |
The definition of an item varies by collection type.
Mistakes with Length and Indices
Treating the length as the final index
Indices start at 0, so a collection with 11 items uses indices 0 through 10.
Fix:
Find the final index by subtracting 1 from the length.Ignoring spaces when counting a string
len() counts every character in a string, including spaces.
Fix:
Count letters, spaces, digits, and punctuation as individual characters.Assuming len() counts the same kind of item for every data type
len() works across collection types, but the definition of item varies by data type.
Fix:
Identify what the particular collection treats as one item before interpreting the result.
Size-Based Practice
A string contains 7 characters, including one space. Determine the highest valid index. Then explain why the space must be included when determining the string's length.
Hints
- Indices begin at 0.
- The highest valid index is one less than the length.
- Every character in a string contributes to len(), including spaces.
Consider a collection whose type is not a string. Before interpreting its len() result, identify what that collection considers one item. Explain how this differs from counting characters in a string.
Hints
- len() works with lists, tuples, dictionaries, and sets.
- The meaning of item varies by data type.
Key Takeaways
- len() returns the number of items in its argument as an integer.
- For strings, every character counts, including spaces and punctuation.
- Indices begin at 0, so the final valid index is always the length minus 1.
- len() also works with lists, tuples, dictionaries, and sets, although the meaning of item varies by data type.