Python Program to Count the Number of Words and Characters in a String

This is a Python Program to calculate the number of words and characters present in a string.

Problem Description

The program takes a string and calculates the number of words and characters present in the string.

Problem Solution

1. Take a string from the user and store it in a variable.
2. Initialize the character count variable to 0 and the word count variable to 1.
3. Use a for loop to traverse through the characters in the string and increment the character count variable each time.
4. Increment the word count variable only if a space is encountered.
5. Print the total count of the characters and words in the string.
6. Exit.

Program/Source Code

Here is source code of the Python Program to calculate the number of words and characters present in a string. The program output is also shown below.

string=raw_input("Enter string:")
char=0
word=1
for i in string:
      char=char+1
      if(i==' '):
            word=word+1
print("Number of words in the string:")
print(word)
print("Number of characters in the string:")
print(char)
Program Explanation

1. User must enter a string and store it in a variable.
2. The character count variable is initialized to zero and the word count variable is initialized to 1 (to account for the first word).
3. The for loop is used to traverse through the characters in the string.
4. The character count is incremented each time a character is encountered and the word count is incremented only when a space is encountered.
6. The total count of characters and the words in the string is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter string:Hello world
Number of words in the string:
2
Number of characters in the string:
11
 
Case 2:
Enter string:I love python
Number of words in the string:
3
Number of characters in the string:
13

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.