Appending strings what am i doing wrong

I am having trouble with all the exercises which involve looping through some sequence and appending it to a list. I have written a sample of how i am trying to achieve this. Can you please tell me when i am going wrong? it feels like i am missing something very obvious.

letters = “ABC”

for i in letters:
a = ‘’
a+=i
print(a)

Expected result : ‘ABC’
Actual Result: ‘C’

Why not fire in some print statements and find out what happens at each step. Have you performed a similar task with a list? Does it look similar to this?

1 Like

Each time through a loop, the code that is inside the loop will run. So if we walk through your loop,

for each letter in letters:
    create an empty string a
    add the current letter to a
    print a

Do you see the bug in that? Your code is very nearly correct, you just have to make one change.

2 Likes