Page 333 - AI Ver 1.0 Class 10
P. 333
1. Write a program to input a string and display the count of vowels and consonants in the string.
Ans. Program:
str=input("Please enter a string as you wish: ")
vowels=0
consonants=0
for i in str:
if(i == 'a'or i == 'e'or i == 'i'or i == 'o'or i == 'u' or i == 'A'or i ==
'E'or i == 'I'or i == 'O'or i == 'U' ):
vowels=vowels+1
else:
consonants=consonants+1
print("The number of vowels:",vowels)
print("\nThe number of consonants:",consonants)
Output:
Please enter a string as you wish: Orange Education
The number of vowels: 8
The number of consonants: 8
2. Write a program to input a string and display the string in the reverse order.
Ans. Program:
def reverse_string(str):
str1 = ""
for i in str:
str1 = i + str1
return str1
str = "ArtificialIntelligence"
print("The original string is: ",str)
print("The reverse string is",reverse_string(str))
Output:
The original string is: ArtificialIntelligence
The reverse string is ecnegilletnIlaicifitrA
3. Write a Python code to take the input of a number n and then find and display its factorial (n!). For example,
5! = 5x4x3x2x1 i.e., 120.
Ans. Program:
n = int(input("Enter a number: "))
Python Practical Questions 331

