Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

How to Find the Length of a String in Python

Finding the length of a String could be useful or even essential for a Python Software Engineer several times a day. It’s rare for someone to be a skilled software engineer if they haven’t mastered how to manipulate and work with strings significantly well. In this article, we’ll learn ways to find the length of a String as well as some other data types in Python using the len() function.

If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Sum Function in Python, and How to Read and Write Files in Python for more specific insights and guidance on Python concepts and coding interview preparation.

Having trained over 9,000 software engineers, we know what it takes to crack the toughest tech interviews. Since 2014, Interview Kickstart alums have been landing lucrative offers from FAANG and Tier-1 tech companies, with an average salary hike of 49%. The highest ever offer received by an IK alum is a whopping $933,000!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

In this article, we’ll discuss:

  • What Is the Length of a String in Python?
  • What Is the len() Method and What Does It Do?
  • How to Find the Length of a String in Python
  • Example: Using len() to Find the Length of a String
  • Why Is Python Considered the Best Programming Language? 
  • len() FAQs

What Is the Length of a String in Python?

The length of a string in Python refers to the number of characters in the string or the size of the string. For example, the length of the string “Interview” is 9 because it has nine characters. 

What Is the len() Method and What Does It Do?

We usually find the length of a string in Python using the built-in len() method, which we will discuss in much more detail in this article as we go forward. We will also discuss some alternate methods.

How to Find the Length of a String in Python

Jumping right into ways you can find a string’s length in Python, here are the most used methods you can use for the purpose:

Using the len() Method to Find String Length in Python

The len() function is a built-in method in Python. It can be used to get the length of not just a string but also a list, a tuple, a dictionary, an array, etc. len() can also help in the performance optimization of the program by calculating the number of elements stored in any desired object. For example, len(“Interview”) will return 9 as it represents the number of characters in “Interview.”

len() Syntax: 


len() Parameters:

The len() method takes a single parameter seqCol, which can be a sequence or a collection. The sequence can be a string, list, bytes, range, tuple, and the collection can be a dictionary, set, etc. 

Return Value From len():

len(seqCol) returns the number of items in the object seqCol. Here are the types of return values you can find:

  • Strings: Returns the number of characters in a string, including counting all special characters like space, @, etc.
  • Collections: Returns the number of elements in a collection.
  • Dictionary: Each pair is considered a single unit, but keys and values are not independent.
  • Empty: Refers to a second return call with zero characters
  • TypeError: Exception raised when an invalid argument is passed.


If the passed argument seqCol is invalid/fails to pass, len() raises a TypeError exception. For example, if you pass no argument, len() raises a TypeError Exception, which you can find in the standard error console.





Traceback (most recent call last):

  File "./prog.py", line 1, in <module>

TypeError: len() takes exactly one argument (0 given)

Using a for Loop to Find String Length in Python

An alternate way of finding the length of a string is by using a for loop and a counter variable initialized to 0. The method involves iterating through the string using the for loop and incrementing the counter in each iteration. 


count = 0

# Iterate through the string 

for t in "Interview":

    #increment count for each iteration


# Outputs the length of the string "Interview"





Example: Using len() to Find the Length of a String

Let us now see the len() function at work and use it to find the length of a string, list, tuple, dictionary, array, range, and set:


import array as arr

strExample = "Interview"

listExample = ["Interview","Kickstart","I","K"]

tupleExample = ('Software','Hardware','System','Management')

dictExample = {'Apples': 10,'Bananas':20,'Chocolate':30}

arrExample = arr.array('i', [1, 2, 3, 4, 5])

rangeExample= range(1,10)

setExample= {'Apples', 20, 'Chocolate', 40}
















Why Python Is Considered the Best Programming Language? 

Python is considered the best programming language because:

  • It emphasizes on readability.
  • It has made coding convenient as it’s high-level, interpreted, and object-oriented, making it great for many different types of software solutions.
  • It is the fastest-growing programming language in the world.

Did you know that Python is one of the most popular programming languages in FAANG companies? Read this article to learn more about the other high-rated languages.

len() FAQs

1. What can we not send as an argument to the len() function in Python?

The len() function only takes a single argument that’s either a sequence or a collection. So we cannot send multiple arguments to the len() function. We can also not send an argument that’s a keyword or an argument that is not a sequence or a collection to the len() function.

2. What does the len() function do in Python?

The len() function tells you the number of items in the sequence or collection passed as the argument.

3. Does len() start at 0 Python? Is len() zero-indexed?

No, len() is not zero-indexed, and its count starts at one since it aims to give the number of elements in a collection. So, len(“a”) will return 1, and len(“”) will return 0.

4. Does len() in Python count spaces in strings?

Yes, len() in Python counts spaces just as it counts all special characters within the string argument. So len(“@ I K”) will return 5.

5. How do I check if a string is empty in Python?

For any string strCheck, you can check if it’s empty by using the len() function on it. If len(strCheck) returns 0, then the string is empty.

Ready to Nail Your Next Coding Interview?

Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, a Tech Lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts