Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Sequences Sequence Iteration Iterating with Basic For Loops

Doubt about printing

Why when Ashley print out her name it's goes from top to bottom with each letter and when I print the groceries list it prints each item one next to the other one?

pet
pet
10,908 Points

Could you post your code please, Federico Lemaire.

4 Answers

rydavim
rydavim
18,814 Points

Python3's print() function ends with a new line by default. If you are seeing different behavior than in the video, that would not be the default behavior and we'll need to take a look at your code. You can post a snapshot of your workspace using the camera icon towards the top right of the window, which will allow us to look at a copy of what you're working on.

If you're not using workspaces, it may have something to do with what version of Python you're using or what the settings on your development environment are like.


Update:

A for loop is actually more like an iterator than in in other languages. It iterates over a sequence - list, tuple, dictionary, set, or string. So in the case of a string, you are iterating over the characters. In a list, you are iterating over the items.

my_name = 'Federico'

for name in my_name: # for each character, print
    print(name)

groceries = ['roast beef', 'cucumbers', 'lettuce', 'peanut butter', 'bread', 'dog food']

for item in groceries: # for each item, print
    print(item)

Learn Python has some good documentation on how for loops work in Python. But if you have any follow up questions, let me know! Happy coding!

Yes! Here is the code

https://w.trhou.se/3tvpz85ys9

This is the result and here is my doubt, why Federico goes from top to bottom and groceries goes one next to the other

F
e
d
e
r
i
c
o
roast beef
cucumbers
lettuce
peanut butter
bread
dog food

Thanks a lot :)

rydavim
rydavim
18,814 Points

Got it. I've updated the answer above.

SERVICIOS ADQUIRIDOS SA DE CV
SERVICIOS ADQUIRIDOS SA DE CV
1,519 Points

Because in the first one you are printing the characters in a STRING and in the second part you are printing the items in a LIST. Think about it like every caracter in a STRING = every item in a LIST

Thanks a lot Rydavim!!! Now I've got it too. May I ask you about other issue that I'm having?

rydavim
rydavim
18,814 Points

If it's related to this, shoot. If it's a new unrelated question it's better to submit a new post so that other students can weigh in or find answers to similar problems. :)

Got it! Submitting a new post :)