- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Calculate the Number of Words and the Number of Characters Present in a String
When it is required to calculate the number of words and characters present in a string,
Below is the demonstration of the same
my_string = "Hi there, how are you Will ? " print("The string is :") print(my_string) my_chars=0 my_words=1 for i in my_string: my_chars=my_chars+1 if(i==' '): my_words=my_words+1 print("The number of words in the string are :") print(my_words) print("The number of characters in the string are :") print(my_chars)
The string is : Hi there, how are you Will ? The number of words in the string are : 8 The number of characters in the string are : 29
A string is defined, and is displayed on the console.
The number of characters is assigned to 0.
The number of words is assigned to 1.
The string is iterated over, and the character variable is incremented.
If a space is encountered, then the word count is also incremented.
These values are displayed on the console.
- Python program to calculate the number of digits and letters in a string
- Java Program to count the number of words in a String
- C# program to count the number of words in a string
- Python program to count number of substring present in string
- PHP program to find the number of characters in the string
- C program to count characters, lines and number of words in a file
- Python Program to Count Number of Lowercase Characters in a String
- Java program to calculate the power of a number
- Count Number of Lowercase Characters in a String in Python Program
- Count the Number of matching characters in a pair of string in Python
- Calculate the Power of a Number in Java Program
- Program to count number of distinct characters of every substring of a string in Python