Exercises 18

Write a program in Python that asks the user to enter a string s and return a message indicating whether the string contains the letter 'a' by indicating its position on the string. Example if the user types the string

s = 'language'

the program returns: The letter

'a'

is in position: 1 The letter

'a'

is in position: 5

Solution

# Ask user to enter a string s
s = input("Type a some string s : ")

# retrieve the len of the string s
n = len(s)

# Browsing through all characters of the string s
for i in range(0,n):
# Try if the browsed character is equal to character 'a'
if(s[i] == 'a'):
print("The letter 'a' is at position : ", i , " in the string s")
Younes Derfoufi
my-courses.net

Leave a Reply