- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python len() Function
The len() function in Python is used to find the length of a sequence such as list, tuple, string, bytes, range or a collection such as dictionary, set, frozenset.
Basically the function len() returns the number items available in an object. For example:
mylist = [32, 3, 65, 76] print("Length of list =", len(mylist)) mytuple = (42, 53, 6) print("Length of tuple =", len(mytuple)) mystring = "fresherearth" print("Length of string =", len(mystring)) mybyte = b'fresherearth' print("Length of byte =", len(mybyte)) myrange = range(15) print("Length of range =", len(myrange)) mydictionary = {"Name": "Maradona", "Country": "Argentina"} print("Length of dictionary =", len(mydictionary)) myset = {32, 43, 54, 56, 76, 8} print("Length of set =", len(myset)) myfrozenset = frozenset(mytuple) print("Length of frozenset =", len(myfrozenset))
The output produced by above Python program, demonstrating the len() function, will exactly be:
Length of list = 4 Length of tuple = 3 Length of string = 12 Length of byte = 12 Length of range = 15 Length of dictionary = 2 Length of set = 6 Length of frozenset = 3
Python len() Function Syntax
Syntax to use len() function in Python is:
len(objectName)
Python len() Function Example
The len() function uses most of the time when we work with list or string in Python. Here is an example:
print("Enter elements for the list (\"stop\" to exit): ", end="") mylist = [] while True: val = input() if val != "stop": mylist.append(val) else: break print("\nThe list is:") for i in range(len(mylist)): print(mylist[i], end=" ") print()
The output produced by above program with some inputs and "stop" at last, to stop receiving inputs, is shown in the snapshot given below:
See how important the len() function is. In above program, the list continue storing the elements entered by user, until user enters "stop". But while printing the elements of the list back on the screen, the len() function find the length of the list, to print all the elements of the list using loop.
This is just a simple program. There are a multiple uses of len() function. Here is another example, uses len() function while working with string in Python:
print("Enter the String: ", end="") str = input() print("\nLength of String =", len(str))
Sample run with user input fresherearth as string is show in the snapshot given below:
The len() function can be used in many programs. But always remember, it is used only to find the length of an object.
« Previous Function Next Function »