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 Basics Functions and Looping For Loops

mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

for loops video, how did the teacher get the result to print like a banner i.e. a new letter on every line without using

for loops video, how did the teacher get the result to print like a banner i.e. a new letter on every line without using the code """\n""" in his code.

3 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,718 Points

Mohan, the Python print() function by default prints a newline '\n' character at the end. This is can be overridden. See the example below:

print("Print each number in the sequence on a different line")
for x in range(10):
    print(x)

print("Now, print on the same line with dashes in between")

for x in range(10):
    print(x,end='-')

When we run the code, we get this output.

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================== RESTART: C:/Users/jeff/Downloads/test.py ==================
Print each number in the sequence on a different line
0
1
2
3
4
5
6
7
8
9
Now, print on the same line with dashes in between
0-1-2-3-4-5-6-7-8-9-
youssef b10ta
PLUS
youssef b10ta
Courses Plus Student 2,755 Points

because he did loop through a string

string = 'HELLO'
for letters in string:
    print(letters)
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

if he did loop through a string how does python program know to print each individual letter on a new line? or is that how python comes?

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,718 Points

This might be what you are looking for...

Normally the Python 3 print function prints on a single line (with a newline at the end), but you can change the "end" parameter and substitute your own parameter. I am using a space character in the example below.

# Python 3 code for printing 
# Team Treehouse Is Tops!

print("Team", end=" ") 
print("Treehouse", end=" ") 
print("Is", end=" ")
print("Tops!")