Python provides multiple methods to find the length of the given string. There is a built-in method len() to find the length of the string.
Syntax:
len(string_name)
Program to find the length of string using len() method
# Python program to find the length of
# the string using len() method
# Getting string input from the user
myStr = input('Enter the string : ')
# Finding the length of the string
strLen = len(myStr)
# printing values
print("Entered String is ", myStr)
print("Length of the string : ", strLen)
Output:
RUN 1:
Enter the string : Hello world.
Entered String is Hello world.
Length of the string : 12
RUN 2:
Enter the string : Hello, Reader! How are you?
Entered String is Hello, Reader! How are you?
Length of the string : 27
Method 2: Using the loop
To find the length of a string, we need to loop over the string and then count the characters. Then print the size.
Algorithm:
Initialise : size = 0
Take string as input from the user.
Loop for all characters in the string,
increment the size -> size++
Print size.
Program to find the length of string using loop
# Python program to find the length
# of the string using loop
# Getting string input from the user
myStr = input('Enter the string : ')
# Finding the length of the string
strLen = 0
for i in myStr:
strLen = strLen + 1
# printing values
print("Entered String is ", myStr)
print("Length of the string : ", strLen)
Output:
Enter the string : IncludeHelp.com
Entered String is IncludeHelp.com
Length of the string : 15
Method 3: Using the string slice method
One more way to find the length of the string is by using a loop with the slice method. We will slice the string by incrementing the counter till the slice makes the string empty.
Program to find the length of string using slice and while
# Python program to find the length of the string
# Getting string input from the user
myStr = input('Enter the string : ')
# Finding the length of the string
strLen = 0
while myStr[strLen:]:
strLen = strLen + 1
# printing values
print("Entered String is ", myStr)
print("Length of the string : ", strLen)
Output:
Enter the string : IncludeHelp
Entered String is IncludeHelp
Length of the string : 11
Method 1: Using the len() method
Python provides multiple methods to find the length of the given string. There is a built-in method len() to find the length of the string.
Syntax:
Program to find the length of string using len() method
Output:
Method 2: Using the loop
To find the length of a string, we need to loop over the string and then count the characters. Then print the size.
Algorithm:
Program to find the length of string using loop
Output:
Method 3: Using the string slice method
One more way to find the length of the string is by using a loop with the slice method. We will slice the string by incrementing the counter till the slice makes the string empty.
Program to find the length of string using slice and while
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer