Interview Kickstart has enabled over 21000 engineers to uplevel.
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:
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.
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.
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:
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(seqCol)
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.
len(seqCol) returns the number of items in the object seqCol. Here are the types of return values you can find:
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.
Example:
len()
Output:
stderr
Traceback (most recent call last):
File "./prog.py", line 1, in <module>
TypeError: len() takes exactly one argument (0 given)
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.
Example:
count = 0
# Iterate through the string
for t in "Interview":
#increment count for each iteration
count+=1
# Outputs the length of the string "Interview"
print(count)
Output:
stdout
9
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}
print(len(strExample))
print(len(listExample))
print(len(tupleExample))
print(len(dictExample))
print(len(arrExample))
print(len(rangeExample))
print(len(setExample))
9
4
4
3
5
9
4
Python is considered the best programming language because:
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.
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.
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!
Attend our webinar on
"How to nail your next tech interview" and learn