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 :

 

Algorithm :

1. Ask the user to enter the string. Read and store it in a variable.

2. 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.

3. Using one loop, iterate through the characters of the string one by one.

4. 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’.

5. 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 :

You can download this program from here

python count total characters in string

 

 

Explanation of the program :

1. 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.

2. ‘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.

3. 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.

4. 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.

5. 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 :

python count total characters in string

 

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.

Categories: python

Leave a Reply

Your email address will not be published. Required fields are marked *