Computer Science Python – Write a program to count number of characters in a given string

This is a very simple program to start with. Python makes it more simple. Let us see what is expected from us, if the user enters the following string, let us say:

Computer

Output should be:    8

We will try to solve this problem in several different ways and also using different types of loops. Lets see the first solution:

string=input("Please Enter the String: ")
counter=0
for x in string:
    counter=counter+1
print(counter)

The program above, performs the following steps:

  • Accepts a string from the user
  • Employs a loop, 
for x in string:

This loops in Python makes it very simple to read the entire string character by character. Loop reads characters one by one from the string entered by the user and places them in the variable x. Inside the loop, we are counting the operation, i.e. every time a character is placed in a variable x, counter in incremented by 1, observe the code below:

 

for x in string:
    counter=counter+1
print(counter)

When all the characters are read, loop ends and we print the value of counter.

We can do it in several other ways, and at the end we will see how simple it can be using one of the python library functions.

Let’s examine another solution, though very similar:

string=input("Please Enter the String: ")
counter=0
for counter in range(0,len(string)+1):
    pass
print(counter)
  • In the program above we are using the range function to run the loop specified number of times (length of the string). Inside function we are not doing anything, since counter is getting incremented on each pass of the loop.
  • Also observe the usage of pass statement inside loop. This is tell python nothing to be done inside loop. To know more about pass statement click on the link: Python statement – pass.

Let’s see how we can solve the same problem using while loop:

string=input("Please Enter the String: ")
counter=0
while counter<len(string):
    counter=counter+1
print(counter)

Above solutions (using Computer Science Python) solve the problem and are designed to do it a traditional programming style. Motive is to force student to apply some logic. Python has so many library functions that can make the job easier, but you may not know what is happening under the hood. Sometime you are asked to perform a task but without using library functions. In all such cases any of the above solution can be used to perform a task.

Still, since there is already a library function and we have already used it in the example above, we will see how to use it to our full advantage. It can actually help, eliminate the use of loop altogether, Observe the example below:

string=input("Please Enter the String: ")
counter=len(string)
print(counter)

So easy, isn’t it?

Happy Coding

Pawan Arora AdministratorKeymaster
Founder , Edukers
Teaching, Coding and Sharing is his passion. A true mentor and motivator. C/C++, Python, Java, Web Technologies (html5 / CSS/ Javascript/ JQuery,Bootstrap, nodeJS ,PHP etc.) and a WordPress enthusiast with more than two decades of experience.
follow me