'How to take string as input and output each letter of the string on a new line, repeated N times, where N is the position of the letter in the string

I was given this problem in a Python class I'm taking. Take a string as input and output each letter of the string on a new line, repeated N times, where N is the position of the letter in the string.

Here is my first attempt:

string = input()

for i in string:
    n = string.index(i) + 1
    print(i * n)

'''

but the output for the input: 'awesome', was:

'''

a
ww
eee
ssss
ooooo
mmmmmm
eee

'''

The output I want is:

'''

a
ww
eee
ssss
ooooo
mmmmmm
eeeeeee

'''

I was able to figure out a different way of doing it, that ended up giving me the correct output:

string = input()
i = 0
while i < len(string):
    
    print(string[i] * (i + 1))
    i += 1

a
ww
eee
ssss
ooooo
mmmmmm
eeeeeee

My question now is why my first attempt does not give me the expected output.



Solution 1:[1]

string.index(i) returns the first index of i, which is simply the letter e in this case (when you call string.index('e'), Python doesn't know you've obtained 'e' as the x'th letter from the same string).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Orius