Python len() Function Last Updated : 15 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example: Python s = "GeeksforGeeks" # Get length of the string l = len(s) print(l) Output13 Explanation: The string "GeeksforGeeks" has 13 characters. The len() function counts and returns this number.Syntax of len() functionlen(object)Parameter: object is a sequence (such as a string, list, tuple) or collection (such as a dictionary, set) whose length is to be calculated.Returns: An integer value indicating the number of items in the object.Examples of using len() functionExample 1: In this example, we are getting the length of a list, tuple and dictionary and printing the result for each. Python a = ['geeks', 'for', 'geeks', 2022] print(len(a)) # List length b = (1, 2, 3, 4) print(len(b)) # Tuple length c = {"name": "Alice", "age": 30, "city": "New York"} print(len(c)) # Dict keys Output4 4 3 Explanation:For the list a, len(a) returns 4 because it contains four items.For the tuple b, len(b) also returns 4 as there are four elements.For the dictionary c, len(c) returns 3 because it counts the number of key-value pairs (i.e., keys).Example 2: In this example, we are getting the length of an empty list and printing the result. Python a = [] # Get the length of the empty list print(len(a)) Output0 Explanation: Since the list is empty, len() returns 0, indicating there are no elements inside it.Example 3: In this example, we are using len() along with a for loop to access and print each element of a list by its index. Python a = [10, 20, 30, 40, 50] # Iterate over list for i in range(len(a)): print("Index:", i, "Value:", a[i]) OutputIndex: 0 Value: 10 Index: 1 Value: 20 Index: 2 Value: 30 Index: 3 Value: 40 Index: 4 Value: 50 Explanation: In this example, range() and len() are used to iterate over the list a by index. len(a) gives the total number of elements and range(len(a)) provides the index sequence. In each iteration, a[i] accesses the value at index i. Comment More infoAdvertise with us Next Article Python len() Function K kamalsagar Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python dict() Function dict() function in Python is a built-in constructor used to create dictionaries. A dictionary is a mutable, unordered collection of key-value pairs, where each key is unique. The dict() function provides a flexible way to initialize dictionaries from various data structures.Example:Pythond=dict(One 4 min read divmod() in Python and its application In Python, divmod() method takes two numbers and returns a pair of numbers consisting of their quotient and remainder. In this article, we will see about divmod() function in Python and its application. Python divmod() Function Syntaxdivmod(x, y)x and y : x is numerator and y is denominatorx and y m 4 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read eval in Python Python eval() function parse the expression argument and evaluate it as a Python expression and runs Python expression (code) within the program.Python eval() Function SyntaxSyntax: eval(expression, globals=None, locals=None)Parameters:expression: String is parsed and evaluated as a Python expressio 5 min read filter() in python The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. Let's see a simple example of filter() function in python:Example Usage of filter()Python# Function to check if a number is even def even(n): return n % 2 == 0 a = [1 3 min read float() in Python Python float() function is used to return a floating-point number from a number or a string representation of a numeric value. Example: Here is a simple example of the Python float() function which takes an integer as the parameter and returns its float value. Python3 # convert integer value to floa 3 min read Python String format() Method format() method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well-organized output. It replaces the outdated % formatting method, making string interpolation more readable and efficient. E 8 min read Python - globals() function In Python, the globals() function is used to return the global symbol table - a dictionary representing all the global variables in the current module or script. It provides access to the global variables that are defined in the current scope. This function is particularly useful when you want to in 2 min read Python hash() method Python hash() function is a built-in function and returns the hash value of an object if it has one. The hash value is an integer that is used to quickly compare dictionary keys while looking at a dictionary.Python hash() function SyntaxSyntax : hash(obj)Parameters : obj : The object which we need t 6 min read hex() function in Python hex() function in Python is used to convert an integer to its hexadecimal equivalent. It takes an integer as input and returns a string representing the number in hexadecimal format, starting with "0x" to indicate that it's in base-16. Example:Pythona = 255 res = hex(a) print(res)Output0xff Explanat 2 min read Like