Returns how many items a string or list has.
Synopsis
len(value)
Description
For a string, len counts the characters, including spaces. For a list, it counts the items.
Parameters
| Name | Meaning |
|---|---|
value |
A string, a list, or another collection. |
Return value
An int.
Example
print(len("hello"))
print(len("hi there"))
print(len(""))
print(len(["a", "b", "c"]))
It prints:
5
8
0
3
Common mistake
print(len(12345))
Python stops with:
TypeError: object of type 'int' has no len()
A number has no length. To count its digits, make it a string first: len(str(12345)).
See also
Last updated 2 weeks ago