Exercise 26
Write a program that asks the user to enter a text and return all words beginning with the letter a.
Solution
First method
# Ask to type a string text
s = input("Type a string text : ")
# convert a string s to list
s = s.split()
# fetching all list element that beginning with the letter 'a'
for x in s:
if(x[0] == 'a'):
print("The word : ", x, " begin with the letter 'a'")
Second method
# Ask to type a string text
s = input("Type a string text : ")
# convert a string s to list
s = s.split()
# Getting the len of list s
n = len(s)
# fetching all list element that beginning with the letter 'a'
for i in range(0,n):
if(s[i][0] == 'a'):
print("The word : '", s[i], "' begin with the letter 'a'")
"""
Example of execution : if you type "Me and you are beginner"
You will receive the message :
The word : 'and' begin with the letter 'a'
The word : 'are' begin with the letter 'a'
"""
Younes Derfoufi