Python 3 program to count the total number of characters in a string
Write a Python 3 program to count the total number of characters in a String :
In this example, we will learn how to write a Python 3 program to count the total number of characters of a string. The program will take one string as input and it will print the total count of all characters available in the string. We are not going to count any space, tabs or newline.
For example, ‘Hello World’ will return 10. Note that one space is in this string between the words, but we are not counting that.
With this program, you will learn how to iterate through a string in python and how to validate if a character is a non-empty character, i.e. it is not space, tab or newline. You will also learn how to use one counter to count the total character count in a string.
The algorithm we are going to use is like below :
- Ask the user to enter the string. Read and store it in a variable.
- Initialize one counter variable and assign zero as its value. We will increment this value by 1 if any character is found in the string.
- Using one loop, iterate through the characters of the string one by one.
- Check each character if it is a blank character or not. If it is not a blank character, increment the value of the counter variable by ‘1’.
- After the iteration is completed, print out the value of the counter. This variable will hold the total number of characters in the string.
Let’s take a look into the program :
Python 3 Program :
input_string = input("Enter a string : ") count = 0 for c in input_string : if c.isspace() != True: count = count + 1 print("Total number of characters : ",count)
You can also download this program from here
Explanation of the program :
- The program will ask the user to enter the string first. We are using the input() method for it. This method takes one argument string. This string will be printed to the user and the program will pause to read the user input.
- ‘count’ is the variable to hold the total count of all characters in the string. It is initialized with ‘0’. input() method returns one string, i.e. it reads the user input value as a string and returns it. In the above program, we are storing this value in the input_string variable.
- We are using one for loop to iterate through all characters of the string. Looping through the characters in a string is really easy in python.
- On each iteration of the loop, we are checking if the current character is a blank character or not by using the ‘isspace()’ method. It returns True if the character is empty. If it is not an empty character, we are incrementing the value counter ‘count’ by 1.
- At the end of the program, we are printing the value of the ‘count’, i.e. the total character count for the given string.
Sample Outputs :
Explanation of the Outputs :
In the above example, the first string contains ‘tabs’ and the second string contains ‘spaces’. You can see that space and tabs are not counted in both cases. Only the total number of characters is printed.
Similar tutorials :
- Python program to remove characters from odd or even index of a string
- Python program to replace character in a string with a symbol
- Python program to convert a list to string
- Python program to find larger string among two strings
- Python program to count the frequency of each words in a string
- Python program to capitalize first letter of each words of a string