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 trialFederico Lemaire
8,423 PointsDoubt 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?
4 Answers
rydavim
18,814 PointsPython3'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!
Federico Lemaire
8,423 PointsYes! Here is the code
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
18,814 PointsGot it. I've updated the answer above.
SERVICIOS ADQUIRIDOS SA DE CV
1,519 PointsBecause 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
Federico Lemaire
8,423 PointsThanks a lot Rydavim!!! Now I've got it too. May I ask you about other issue that I'm having?
rydavim
18,814 PointsIf 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. :)
Federico Lemaire
8,423 PointsGot it! Submitting a new post :)
pet
10,908 Pointspet
10,908 PointsCould you post your code please, Federico Lemaire.