Word and character counter; Python Programming

word count

This short program asks a user to input a string. It then counts each character and each complete word the user enters. Finally it prints this information to the screen.

We will break the program down a bit to start so as not to confuse ourselves. We will start with getting the user information and counting the number of characters entered.

We ask the user to enter a string and store it in a variable we called sentence

sentence = input(“Enter a string:”)

Next we create a variable called chars to count the number of characters entered. We assign this the value 0 as it will be empty to start.

chars = 0

Next we create a variable called words and assign it the value 1. We do this as the program will only start counting after the first word is created. We need to add the first word to the count to keep it accurate.

words = 1

Now we need a for loop to iterate through the variable sentence (where the string is stored). We will ask it to check each character in the variable sentence. For each character it counts in the variable sentence; add 1 to the variable chars and assign that to itself.

for characters in sentence:

        chars = chars + 1

 

Print the text and the variable chars to the screen;

 

print(“Number of characters in the string:”, chars)

 

Try running this program and see if it works;

 

sentence = input(“Enter a string:”)

chars = 0

words = 1              # will be used in the next part of the program

for characters in sentence:          

        chars = chars + 1

        print(“Number of characters in the string:”, chars)

 

Now we need to also count the words. We already have the variable words assigned to 1. The only extra bit we need to add is an if statement looking for a space between characters. If we find a space we can safely assume it’s a new word.

for characters in sentence:

        chars = chars + 1

       if(characters == ‘ ‘):          # looks for an empty space.                                    

               words = words+1    # adds 1 to the word count

 

lastly we add a print statement for the amount of words

print(“The number of words in the string is:”, words)

 

Now for the full program:

sentence = input(“Enter a string:”)

sentence = input(“Enter a string:”)

chars = 0

words = 1

for characters in sentence:

        chars = chars + 1

       if(characters == ‘ ‘):

                words = words+

print(“The number of words in the string is:”, words)

print(“Number of characters in the string:”, chars)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s