Python Program to Count Total Characters in a String

Write a Python program to Count Total Characters in a String using For Loop and While Loop with a practical example.

Python Program to Count Total Characters in a String using for loop

This program allows the user to enter a string. Next, it counts the total number of characters inside this string using For Loop.

Here, we used Python For Loop to iterate every character in a String. Inside the For Loop, we are incrementing the total value for each character.

str1 = input("Please Enter your Own String : ")
total = 0
 
for i in str1:
    total = total + 1
 
print("Total Number of Characters in this String = ", total)
Python Program to Count Total Characters in a String 1

Using the for loop range function

This String Characters program is the same as the above example. However, in this Python code, we are using the For Loop with Range.

str1 = input("Please Enter your Own String : ")
total = 0
 
for i in range(len(str1)):
    total = total + 1
 
print("Total Number of Characters in this String = ", total)

Count String Characters output

Please Enter your Own String : Tutorial Gateway
Total Number of Characters in this String =  16

Python Program to Count Number of Characters in a String using while loop

This Python program to count characters is the same as above. However, we just replaced the For Loop with While Loop.

str1 = input("Please Enter your Own Text : ")
total = 0
i = 0

while (i < len(str1)):
    total = total + 1
    i = i + 1

print("Total Number of Characters = ", total)
Please Enter your Own Text : Hello World
Total Number of Characters =  11

Python Program to count characters in a string using Functions

def totalChars(txt):
    count = 0
    for i in txt:
        count += 1
    return count

txt = input("Enter a Text = ")

print("Total = ", totalChars(txt))
Enter a Text = hello world!
Total =  12

Using recursion

def totalChars(txt):
    if txt == '':
        return 0
    else:
        return 1 + totalChars(txt[1:])

txt = input("Enter a Text = ")

print("Total = ", totalChars(txt))
Enter a Text = Good Day for all of you
Total =  23

Python Program to Count Characters in a String using len and sum functions

This programming language has a built-in string function len() to get the string length.

txt = input("Enter a Text = ")

count = len(txt)

print("Total = ", count)
Enter a Text = welcome
Total =  7

Using list comprehension

The list comprehension converts the string to a list, and then it uses the list len function to find the total items.

txt = input("Enter a Text = ")

count = len([char for char in txt])

print("Total = ", count)
Enter a Text = Jhon Wick
Total =  9

Using sum() function

txt = input("Enter a Text = ")

count = sum(1 for char in txt)

print("Total = ", count)
Enter a Text = Jhony Williams
Total =  14

Using lambda and map function

txt = input("Enter a Text = ")

tot = sum(map(lambda char: 1, txt))

print("Total = ", tot)
Enter a Text = New York
Total =  8