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:
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 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.
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.
# 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.
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.
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:
Python string length | len() function to find string length – FAQs
What is the len() Function in Python?
The
len()function in Python is a built-in function that returns the number of items in an object. When the object is a string, thelen()function returns the number of characters in the string. It can also be used with other iterable types, such as lists, tuples, dictionaries, and sets, to determine the number of elements contained within them.
Which Length Function is Used to Find the Length of the String?
In Python, the
len()function is used to find the length of a string. It counts all characters in the string, including letters, digits, punctuation, and whitespace characters, and returns the total count.Example:
my_string = "Hello world"
length = len(my_string)
print(length) # Output: 11
What is String Size and String Length?
- String Size: Typically refers to the amount of space (in bytes) a string occupies in memory. This is more relevant in languages where you need to manage memory explicitly or where the string encoding affects its size in memory.
- String Length: Refers to the number of characters in the string. This is what functions like
len()in Python measure. The length of the string is the total count of its characters.In Python, the distinction between size and length might come into play with different encodings or with data types that include binary data, but for ordinary strings,
len()gives you the count of characters, which is usually what’s meant by length.
Does len() Start from 0?
The
len()function does not start counting from 0. It returns the total number of elements in the object. For example, if a string has 10 characters,len()returns 10. The index of elements in Python starts from 0, but thelen()function returns the count of total elements.
Does String len() Count Spaces?
Yes, the
len()function counts spaces as characters when calculating the length of a string. Every character, including spaces, punctuation, special characters, and normal alphabetic or numeric characters, is counted as one towards the total length.Example:
my_string = "Hello world" # Includes a space between 'Hello' and 'world'
print(len(my_string)) # Output: 11, including the spaceThese uses and behaviors of the
len()function make it a versatile tool for working with data structures in Python, providing a simple way to obtain their sizes.

