Python find length of string or list using len()

Python len can be used to return length of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

Usage

len(s)

Example – len() – string length

s = "Hello"
print len(s)
5
Env: Python 2.7.6

Example – len() – list length

s = ["item1", "item2", "item3"]
print len(s)
3
Env: Python 2.7.6

Example – len() – dictionary length

s = {"key":"item1", "key2":"item2", "key3":"item3"}
print len(s)
3
Env: Python 2.7.6
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments