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] Displaying text length instead of the actual text.

Here is my code: I'm trying to display the 5 oceans, each in a new line, but when I run it, it displays the character length for each ocean, and not the actual text.

>>> oceans = ['Pacific', 'Atlantic', 'Indian', 'Southern', 'Arctic']
>>> with open('oceans.txt', 'w') as f:
...     for ocean in oceans:
...             f.write(ocean)
...
7
8
6
8
6

Any idea why it is doing this?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

This is correct behavior:

f.write(string) writes the contents of string to the file, returning the number of characters written. (see docs: methods of file objects)

The file is getting written with “PacificAtlanticIndianSouthernArctic”

Post back if you need more help. Good luck!!!