Javaexercise.com

Python Program To Find The Length Of A String In Python

In this article, we are going to see how we can find the length of the string in Python. We created several python programs to get the length of a string. Let's understand the topic with the help of some simple examples.

Input Examples:

Input: "Virat"
Output: 5
​
Input: "Rohit Sharma"
Output: 12
​
Input: "How are you?"
Output:12

The different ways to find the length of a string in python:

  1. Using len() function

  2. Using for loop

  3. Using while loop

  4. Using string method join and count

Find the length of String Using len() function in Python

We can use the built-in function len() which will calculate the length of the string and then return that length.

Algorithm

Step 1: Create a string variable

Step 2: Use the len() function

Step 3: Print the length of a string

See the below Python code implementation of the above algorithm.

# Python program to find the length of the string using len()
​
# strings
str1 = "virat"
str2 = "Rohit Sharma"
str3 = "How are you?"
​
# print the length of strings
print(f"Length of the string 1: {len(str1)}")
print(f"Length of the string 2: {len(str2)}")
print(f"Length of the string 3: {len(str3)}")

​Output:

Length of the string 1: 5

Length of the string 2: 12

Length of the string 3: 12

Find the length of String Using for loop in Python

In this method, we will use a for loop to iterate each letter in a string, and then we will add 1 each time when the loop runs.

Algorithm

Step 1: Define a function that returns the length of the string

Step 2: Declare a variable to store the length of the string and set it to 0

Step 3: Run the for loop to iterate each letter of the string

Step 4: Add 1 to that variable each time when the loop runs

Step 5: Return the length

Step 6: Print the length of the string

See the below Python code implementation of the above algorithm.

# Python program to find the length of the string using for loop
​
# function that returns the length of string
def findLen(str):
    length = 0
    # Iterate each letter in str
    for i in str:
        length += 1
    return length
​
​
# string
str = "virat"
​
# call the function and print the length
print(f"The length of the string: {findLen(str)}")

Output:

The length of the string: 5

Find the length of String Using while loop in Python

In this method, we will find the length of the string by using a while loop and slicing. Here, each time string will become shorter by one when the loop runs and eventually results in an empty string. After that while loop stops.

Algorithm

Step 1: Define a function that returns the length of the string

Step 2: Declare a variable to store the length of the string and set it to 0

Step 3: Run the while loop and use slicing for ending the while loop

Step 4: Add 1 to that variable each time when the loop runs

Step 5: Return the length

Step 6: Print the length

See the below Python code implementation of the above algorithm.

# Python program to find the length of the string using while loop and slicing
​
# function that returns the length of string
def findLen(str):
    length = 0
    
    while str[length:]:
        length += 1
    return length
​
​
str = "How are you?"
​
print(f"The length of the string: {findLen(str)}")

​Output:

The length of the string: 12

Find the length of String Using string methods join and count in python

In this method, we will find the length of the string by using string methods join and count.

Here, we will take any random string and use the join method to join it with the original string whose length we want to measure.

The join method will join this random string after each letter of the original string. Then we will use the count method to count the occurrence of the random string in the resultant string and after that, we will add 1 to it which will give us the length of the original string.

Algorithm

Step 1: Define a function that returns the length of the string

Step 2: Use join and count methods and add 1

Step 3: Return the length

Step 4: Print the length

See the below Python code implementation of the above algorithm.

# Python program to find the length of the string using join and count
​
# Function that returns length of string
def findLen(str):
    if not str:
        return 0
    else:
        some_random_str = 'py'
        return ((some_random_str).join(str)).count(some_random_str) + 1
​
​
# string
str = "Virat"
​
# call the function and print the length of the string
print(f"The length of the string is: {findLen(str)}")

Output:

The length of the string is: 5