;

Python Program to Get the Length of a Given String


Tutorialsrack 28/04/2020 Python

In this Python program, we will learn how to get the length of a given string. We will find the length of the string using the loop and by using a python built-in function len().

Here is the source code of the program to get the length of a given string in Python.

Program 1: Python program to calculate the length of a String without using len() function

Python program to calculate the length of a String without using len() function
# Python program to calculate the length of a String without using len() function

# To take the input from the user
mystr = input("Enter a string: ")

# counter variable to count the character in a string
counter = 0
for s in mystr:
      counter = counter+1
print("Length of the input string is:", counter)
Output

Enter a string: Tutorialsrack.com is an online learning website

Length of the input string is: 47

 

Enter a string: Program 1: Python program to calculate length of a String without using len() function

Length of the input string is: 86

Program 2: Program to find the length of string using built-in function len()

Program to find the length of string using built-in function len()

# Program to find the length of string using library function

# To take the input from the user
mystr = input("Enter a string: ")

# using len() function to find length of str
print("Length of the input string is:", len(mystr))
Output

Enter a string: Program 1: Python program to calculate length of a String without using len() function

Length of the input string is: 86

 

Enter a string: counter variable to count the character in a string

Length of the input string is: 51


Related Posts



Comments

Recent Posts
Tags