;

Python Program to Count the Total Characters in a String


Tutorialsrack 27/04/2020 Python

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.

Program 1: Python Program to Count the Total Characters in a String Using For Loop

Python Program to Count the Total Characters in a String Using For Loop
# 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)
Output

Enter the String: Tutorials Rack is an Online Learning Website

Total Number of Characters in the given String =  44

Program 2: 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

# 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)
Output

Enter The String: Tutorials Rack is an Online Learning Website

Total Number of Characters in this String =  44

Program 3: Python Program to Count the Total Characters in a String Using While Loop

Python Program to Count the Total Characters in a String Using While Loop
# 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)
Output

Enter The String: Tutorials Rack is an Online Learning Website

Total Number of Characters in this String =  44


Related Posts



Comments

Recent Posts
Tags