In this Python tutorial, we will learn various methods to find the length of a string in Python. We’ll use some built-in functions to understand different approaches to finding the size of a string in Python.
As a Developer, while making the Python Project I got the requirement to find the length of a string in Python.
Here we will see:
- How to find the length of a string in Python using len()
- How to find the length of a string in Python using for loop
- How to find the length of a string in Python using a loop and slicing
- How to find the length of a string in Python using join() and count()
- How to find the length of a string in Python using a while loop and slicing
How to find the length of a string in Python
In Python, there are primarily five methods that are commonly used and important to understand to find the length of a string in Python.
How to find the length of a string in Python using len()
- In this section, we will discuss how to find the length of a string in Python using len().
- Python has a built-in function called len(). To determine the length of a given string, array, list, tuple, dictionary, etc., use the len() function. Len function can be used to enhance program performance. Len assists in providing the number of elements because the number of elements stored in the object is never determined.
Syntax:
Here is the Syntax of the len() function in Python
len(value)
Note: This parameter takes only one parameter that shows that you want the find the length of a string.
Example:
Let’s take an example and check how to find the length of a string in Python using len().
Source Code:
# Input string
new_str = "Python is a Programming language"
# Display the Content
print("The length of the input string is :", len(new_str))
In the following given code first, we created a string, and then we checked the length of the string by using the len() function.
Here is the implementation of the following given code.
This is how to find the length of a string in Python using len().
Read: Python format number with commas
How to find the length of a string in Python using for loop
- Now let us understand how to find the length of a string in Python using for loop.
- To perform this particular task we are going to use for loop Instead of using the len() function, you can determine the length by iterating the string through a for-loop while increasing a counter variable each time.
Example:
Let’s take an example and check how to find the length of a string in Python using for loop.
new_count = 0
for i in "USA":
new_count+=1
print (new_count)
In the above code first, we declared a variable ‘new_count’ and then length by iterating the string through a for loop while increasing a counter variable each time.
Here is the Screenshot of the following given code.
As you can see in the Screenshot we have discussed how to find the length of a string in Python using for loop.
Read: Python find number in String
How to find the length of a string in Python using a loop and slicing
- In this section, we will discuss how to find the length of a string in Python using a loop and slicing.
- The while loop and the slicing concept will be used in this method. Until we reach an empty string, we will slice a string in half, reducing its length by 1 each time. terminate the while loop. The length of the string will depend on how many times the loop has been repeated.
Example:
Here we will take an example and check how to find the length of a string in Python using a loop and slicing.
Source Code:
def str(i):
z= 0
while i[z:]:
z=z+1
return z
i= "China, England, Germany"
print("Size of string: ",str(i))
You can refer to the below Screenshot.
In this example, we have understood how to find the length of a string in Python using a loop and slicing.
Read: Remove character from string Python
How to find the length of a string in Python using join() and count()
- In this section, we will discuss how to find the length of a string in Python using join() and count().
- To get the length of a string, we will combine the join() and count() methods in one function. The concatenation of two strings is what the join() function will return. The number of characters in a string is returned by the count() function.
- The given string and a new empty string will be concatenated using the join() method, and we’ll then count the characters in the new string. We will deduct 1 from the final count because the count() method counts the empty string as space and adds an extra count of 1.
Example:
def findLen(s):
if not s:
return 0
else:
st= ''
return ((st).join(s)).count(st)-1
s= "This"
print("Size of string: ",findLen(s))
Here is the implementation of the following given code.
This is how to get the length of a string in Python using join and count.
Read: How to trim a string in Python
How to find the length of a string in Python using a while loop and slicing
- Here we will discuss how to fetch the length of a string in Python using a while loop and slicing.
- At some point, an empty string will result from repeatedly breaking a string in half, removing one character at a time. At this point, the while loop ends.
Example:
def new_len(Cars_in_usa):
i = 0
while Cars_in_usa[i:]:
i += 1
return i
Cars_in_usa = "BMW, VOLKSWAGEN, AUDII"
print(new_len(Cars_in_usa))
Here is the Screenshot of the following given code
You may also like to read the following Python tutorials.
- Python find substring in string
- Find Last Number in String in Python
- Find first number in string in Python
- How to concatenate strings in python
- Python remove substring from a String
In this article, we have discussed various methods to get the length of a string in Python. And also we have covered the following given topics.
- How to find the length of a string in Python using len()
- How to find the length of a string in Python using for loop
- How to find the length of a string in Python using a loop and slicing
- How to find the length of a string in Python using join() and count()
- How to find the length of a string in Python using a while loop and slicing
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.