If you’re posting code to the forums please see- How do I format code in my posts? as it makes it much easier for others to read. If you iterate through a single string it will treat character as an element, for example-
for letter in "this string":
print(letter)
# prints out each letter on by one
Now contrast with the output of using the .split
method-
my_string_split = "this string".split()
print(my_string_split)
Out: ['this', 'string']
All good so far? Can you now see what item is being appended?
I strongly encourage you to use both the docs and liberal use of the print
function to find out exactly what happens at each stage of your code. This kind of debugging is exceedingly helpful when you’re just starting out and what’s more, you’ll probably continue using something similar from now onwards.
Loops and functions in particular often cause headaches as it isn’t always clear what goes on in inside them so many times newcomers resort to guesswork. That’s a sure-fire way to spend longer than you need to on a problem whilst also dramatically increasing the chance of bugs in the long term. Get some print statements in there and it’s no longer a black box, you can track the logic flow and the arguments passed as closely as you feel like.