Open In App

Python len() Function

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Python len() function is an inbuilt function in Python. It can be used to find the length of an object. 

Python len() function Syntax:

len(Object)

Parameter:

  • Object: Object of which we have to find the length for example string, list, etc.

Returns: It returns the integer value which indicates the length of an object.

For example, we can find the total number of characters in a string using the Python len() Function. It can be used with different types of data types except some of them.

Example:

Input: "geeksforgeeks"
Output: 13
Explanation: The input string contains 13 characters so output is 13.

Find the length of built-in Sequences

A container having ordered objects is called a sequence. Python has three fundamental built-in sequences: lists, tuples, and strings. We can find the length of these objects by using Python len() Function. Let’s take some examples:

Code:

Python3




list1 = ['geeks','for','geeks',2022]
# printing length of list1
print(len(list1))
 
tuple1 = (1,2,3,4)
# printing length of tuple1
print(len(tuple1))
 
string1 = "geeksforgeeks"
# printing length of string1
print(len(string1))


Output:

4
4
13

Examining other built-in data types with len()

Not all built-in data types can be used as arguments for len(). The concept of length is irrelevant for data types that only store a single object. The same is true for Boolean types and numbers. Let’s take some examples to understand it.

Python3




num = 10
# Printing length of num
print(len(num))
 
d = 9.9
# Printing length of float number 'd'
print(len(d))
 
boolean = True
# Printing length of boolean
print(len(boolean))
 
c = 7 + 8j
# Printing length of complex number 'c'
print(len(complex1))


Output:

Traceback (most recent call last):
  File "/home/9f7b2a9d90c3912d0e6fd0e4825f42fd.py", line 3, in <module>
    print(len(num))
TypeError: object of type 'int' has no len()

Traceback (most recent call last):
  File "/home/3f0127a097b085c2d840a24761d240cd.py", line 7, in <module>
    print(len(d))
TypeError: object of type 'float' has no len()

Traceback (most recent call last):
  File "/home/f1ca2dfd7b853e17f9abb93b1e822cd6.py", line 11, in <module>
    print(len(boolean))
TypeError: object of type 'bool' has no len()

Traceback (most recent call last):
  File "/home/b5a486420afaeff59dc5dbb7f4341ab3.py", line 15, in <module>
    print(len(complex1))
NameError: name 'complex1' is not defined

As we can see in the above output if we are trying to print the length of built-in data types such as int, float, and bool using Python len() function it gives an error.



Previous Article
Next Article

Similar Reads

Internal Working of the len() Function in Python
The len() function in Python has a very peculiar characteristic that one had often wondered about. It takes absolutely no time, and equal time, in calculating the lengths of iterable data structures(string, array, tuple, etc.), irrespective of the size or type of data. This obviously implies O(1) time complexity. But have you wondered How? Python f
2 min read
Python string length | len() function to find string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method. Example: C/C++ Code string = &quot;Geeksforgeeks&quot; print(len(string)) Output13String len() Syntax len(string) Parameter String: string of which you want to find the length. Return: It returns
3 min read
Find the Length of a String Without Using len Function in Python
In this program, we are given a string and we have to find out the length of a string without using the len() function in Python. In this article, we will see how we can find the length of a string without using len() function in Python. Example : Input: 'abc' Output : 3 Explanation: Length of 'abc' is 3 Python Program to Calculate the Length of a
3 min read
Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
Some of the string methods are covered in the set 3 below String Methods Part- 1 More methods are discussed in this article 1. len() :- This function returns the length of the string. 2. count("string", beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning posi
4 min read
List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3. max() :- This function returns the maximum eleme
2 min read
Python | Pandas Series.str.len()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.len() method is used to determine length of each string in a Pandas series. This method is only for series of strings. Since
3 min read
Python Tuple - len() Method
While working with tuples many times we need to find the length of the tuple, and, instead of counting the number of elements with loops, we can also use Python len(). We will learn about the len() method used for tuples in Python. Example: C/C++ Code Tuple =( 1, 0, 4, 2, 5, 6, 7, 5) print(len(Tuple)) Output : 8Python tuple len() Method SyntaxSynta
2 min read
Write your own len() in Python
The method len() returns the number of elements in the list or length of the string depending upon the argument you are passing. How to implement without using len(): Approach: Take input and pass the string/list into a function which return its length.Initialize a count variable to 0, this count variable count character in the string.Run a loop ti
2 min read
Get Length of a List in Python Without Using Len()
Python len() method is the most common and widely used method for getting the length of a list in Python. But we can use other methods as well for getting the length or size of the list. In this article, we will see how to find the length of a list in Python without using the len() function. Find The Length Of A List In Python Without Using Len()Be
2 min read
Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples. What is Calling a Function
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg