Solution Exercise 16 iterate through the characters of a string in Python
Exercise 16 Write a python program to display all characters of a string variable. Example for s = "Python", the program displays the characters: P y t h o n Solution (First method)
1 2 3 4 5 6 7 8 9 |
# Ask the user to enter a string variable s s = input("Enter a string of characters s: ") # get the length of s n = len(s) # display all characters of s for i in range(0, n): print(s[i]) |
Solution (second method)
1 2 3 4 5 6 |
# Ask the user to enter a string variable s s = input("Type a string of characters s: ") # display all characters of s for x in s: print(x) |
Younes Derfoufi CRMEF OUJDA