by BehindJava
Write a program to Count the Number of Vowels in a String in Python
In this tutorial we are going to learn about counting the Number of Vowels in a String in Python.
Python Program to Calculate the Number of Words and the Number of Characters Present in a String
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)
Explanation
- User must enter a string and store it in a variable.
- The character count variable is initialized to zero and the word count variable is initialized to 1 (to account for the first word).
- The for loop is used to traverse through the characters in the string.
- The character count is incremented each time a character is encountered, and the word count is incremented only when a space is encountered.
- The total count of characters and the words in the string is printed.