In this Python program, we will learn how to count the total characters in a string. In this program, we will use for
and while
loop and for loop with range()
function to count the total characters in a string.
Here is the source code of the program to count the total characters in a string.
# Python Program to Count the Total Characters in a String Using For Loop
# Take the Input From the User
str1 = input("Enter the String: ")
total = 0
for i in str1:
total = total + 1
print("Total Number of Characters in the given String = ", total)
Enter the String: Tutorials Rack is an Online Learning Website
Total Number of Characters in the given String = 44
Python Program to Count the Total Characters in a String Using For Loop with Range
# Python Program to Count the Total Characters in a String Using For Loop with Range
# Take the Input From the User
str1 = input("Enter The String: ")
total = 0
for i in range(len(str1)):
total = total + 1
print("Total Number of Characters in this String = ", total)
Enter The String: Tutorials Rack is an Online Learning Website
Total Number of Characters in this String = 44
# Python Program to Count the Total Characters in a String Using While Loop
# Take the Input from the User
str1 = input("Enter The String: ")
total = 0
i = 0
while(i < len(str1)):
total = total + 1
i = i + 1
print("Total Number of Characters in this String = ", total)
Enter The String: Tutorials Rack is an Online Learning Website
Total Number of Characters in this String = 44
Comments