Exercise 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 input a string s
s = input("Type a string s : ")

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

# Browsing and reading all characters of 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
One thought on “Solution Exercise 18”

Leave a Reply