Jordan youngCourses Plus Student 5,250 Points
Iterating with Basic For Loop
I solved the challenge no problem, but am trying to wrap my head around the process and the code.
Using Ashley's example of printing her own name, the variable "letter" that we created new to print by character. However in the groceries example, the same statement (with a diff variable) prints out each item in quotes.
How does the compiler know that we wanted the items and not the characters within the quotes printed individually, or something else to that effect? Using my former C# brain, I could see if we assigned a print char or print string and how that would be different, but we don't do either in Python.
Brandon WhiteTreehouse Moderator
A string in python is an iterable, so you could perceive it like a list.
my_name = “Ashley” is in many ways the same as
my_name = [“A”, “s”, “h”, “l”, “e”, “y”]
If you wanted to print the characters of each string element in the groceries list, you’d use a nested for loop like so...
for item in groceries: for letter in item: print(letter)
Hopefully that makes sense. Great question!