Given a string, write a Python program to print each character of the string and its corresponding index in the string.
Example 1:
Input: "Hello"
Output: H 0, e 1, l 2, l 3, o 4
Example 2:
Input: "Python"
Output: P 0, y 1, t 2, h 3, o 4, n 5
You can use a for loop and the built-in enumerate function to iterate through the string.
def print_characters_and_indices(s):
for i, char in enumerate(s):
print(char, i)
print_characters_and_indices("Hello")
NOTEOwing to browser caching, any code input into the Trinket IDE might carry over across page refreshes or when transitioning between different questions. To commence with a clean slate, either click on the 'Reset Button' found within the IDE's Hamburger icon (☰) menu or resort to using Chrome's Incognito Mode.