Open In App

Python string length | len() function to find string length

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Python3




string = "Geeksforgeeks"
print(len(string))


Output

13

String len() Syntax

len(string) 

Parameter

String: string of which you want to find the length.

Return:

It returns an integer which is the length of the string. 

What is the String len() Function?

The string len() function is a built-in function in Python that is used to find the length of the string. You can know the number of characters in a string using this function.

It is a very important string function, and it is very easy to use.

How to Use String len() Function?

You can easily use the String len() function, just pass the string as the parameter in the function. Let’s look at an example to understand how to find the length of the string in Python.

More Examples of String len() Function

The len() method is used to find the length of an object in Python. By using len(), we can find the length of strings and lists. In this example, we are finding the length of a string.

Example 1: Len() function with tuples and string

In this example, we are counting the length of the tuples and list.

Python




# Python program to demonstrate the use of
# len() method 
 
# with tuple
tup = (1,2,3)
print(len(tup))
 
# with list
l = [1,2,3,4]
print(len(l))


Output

3
4

Example 2: Python len() TypeError

In this example, we are using len() with a boolean value and find TypeError as a result.

Python3




print(len(True))


Output

TypeError: object of type 'bool' has no len()

Example 3: Python len() with dictionaries and sets

In this example, we are using Python len() with dictionaries and sets to find the length of dictionary and set.

Python3




# Python program to demonstrate the use of
# len() method 
 
dic = {'a':1, 'b': 2}
print(len(dic))
 
s = { 1, 2, 3, 4}
print(len(s))


Output

2
4

Example 4: Python len() with custom objects

In this example, we are using len() to find the length of custom objects and to find the length of an object in Python.

Python3




class Public:
    def __init__(self, number):
        self.number = number
    def __len__(self):
        return self.number
     
obj = Public(12)
print(len(obj))


Output

12

Example 5: Python len() with range()

In this example, we are using the len() function to find the length of the range() function.

Python3




l=range(1,10)
print(len(l))


Output

9

In this article, we have covered about Python string len() function and discussed its working. len() function is used to find the length of a string. It is a very common yet important function of string methods.

Read More String Methods

Also Read:



Previous Article
Next Article

Similar Reads

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
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
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 len() Function
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 tot
2 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
Maximum length of consecutive 1's in a binary string in Python using Map function
We are given a binary string containing 1's and 0's. Find the maximum length of consecutive 1's in it. Examples: Input : str = '11000111101010111' Output : 4 We have an existing solution for this problem please refer to Maximum consecutive one’s (or zeros) in a binary array link. We can solve this problem within single line of code in Python. The a
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg