by BehindJava

Write a program to Count the Number of Vowels in a String in Python

Home » python » 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

  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.
  5. The total count of characters and the words in the string is printed.