Exercise 18
Write a program in Python that asks the user to enter a string of characters s and send back a message indicating whether the string contains the letter 'a' while indicating its position on the string. Example if the user types the string s = 'language' the program returns:
The letter 'a' is at position: 1
The letter 'a' is at position: 5
Solution
1 2 3 4 5 6 7 8 9 10 11 |
# Ask the user to type the value of s s = input("Type the value of s: ") # Get length of string s n = len(s) # Iterate through the string s while searching for the character 'a' for i in range(0,n): # Test if the encountered character is equal to 'a' if(s[i] == 'a'): print("The character 'a' is at position: ", i , " in the string s") |
Younes Derfoufi
CRMEF OUJDA
2 thoughts on “Solution Exercise 18: position of a character in a Python string”