You are on page 1of 8

Assignment

#2
1.Write a program in Python to count and display the number of vowels and
consonants present in a string which is entered by the user.

Code:
s=input("Enter a string: ")
c=0
v=0
for i in s:
if i in 'aeiouAEIOU':
v+=1
if i not in "aieouAIEOU":
c+=1
print("No. of vowels :",v)
print("No. of consonants :",c)

Output:
Enter a string: Hello! My name is Arya
No. of vowels : 7
No. of consonants : 15
>>>
2.Write a program in Python to count the number of occurrences of the
words “is” and “are” in a string which is input by the user.

Code:
l1=input("Enter a string: ")
a=(l1.split())
l2=[]
l3=[]
for i in range(len(a)):
if a[i]=="is":
l2.append(a[i])
elif a[i]=="are":
l3.append(a[i])
print("No. of is :",len(l2))
print("No. of are :",len(l3))

Output:
Enter a string: This is a tree. Those are buildings.
No. of is : 1
No. of are : 1
>>>
3.Write a program in Python to count and display the words ending with ‘y’
or ‘ing’ in a string.

Code:
mystring=input("Enter a string: ")
mylist=mystring.split()
no_words=0
no2_words=0
print("The words ending with 'y' and 'ing' are:")
for i in mylist:
if(i[-1]=='y'):
no_words+=1
print(i)

if(i.endswith('ing')==True):
no2_words+=1
print(i)

print("The count of words ending with 'y' is=",no_words)


print("The count of words ending with 'ing' is=",no2_words)

Output:
Enter a string: Why are you playing in the dining room?
The words ending with 'y' and 'ing' are:
Why
playing
dining
The count of words ending with 'y' is= 1
The count of words ending with 'ing' is= 2
>>>
4.Write a program in Python to find words in a string which are palindromes.

Code:
l=[]
st=input("Enter a string: ")
for word in st.split():
if word==word[::-1]:
l.append(word)
print("The palindrome words in the sentence: ",l)
print("Total number of palindrome words in the sentence: ",len(l))

Output:
Enter a string: mom and dad speak malayalam
The palindrome words in the sentence: ['mom', 'dad']
Total number of palindrome words in the sentence: 2
>>>
5.Write a program in Python to display all the words, which contain “tion” in
it.

Code:
l=[]
st=input("Enter a string: ")
print("Words in the given string which contain 'tion' are:")
for word in st.split():
if (word.__contains__('tion')==True):
print(word)
l.append(word)

Output:
Enter a string: Cation is a negatively charged ion while Anion is a
positively charged one.
Words in the given string which contain 'tion' are:
Cation
>>>
6.Write a program in Python to display the content of each word of a string
in reverse order.

Code:
string =input("Enter a string: ")
words = string.split()
words = list(reversed(words))
print(" ".join(words))

Output:
Enter a string: I am Radhikaa. I am from Delhi
Delhi from am I Radhikaa. am I
>>>
7.Write a program in Python to count the number of words which start with
a vowel in a string.

Code:
mystring=input("Enter a string: ")
mylist=mystring.split()
count=0
print("The words which start with a vowel are:")
for i in mylist:
if(i.startswith('a')==True or i.startswith('e')==True or
i.startswith('i')==True
or i.startswith('o')==True or i.startswith('u')==True or
i.startswith('A')==True
or i.startswith('E')==True or i.startswith('I')==True or
i.startswith('O')==True or
i.startswith('U')==True):
count+=1
print(i)

print("The count of words which start with a vowel are:",count)

Output:
Enter a string: India is located in South East Asia.
The words which start with a vowel are:
India
is
in
East
Asia.
The count of words which start with a vowel are: 5
>>>

You might also like