Python Python Basics Functions and Looping For Loops

mohan Abdul
PRO
mohan Abdul
Pro Student 972 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 23,698 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!")
youssef b10ta
PRO
youssef b10ta
Pro Student 2,754 Points

because he did loop through a string

string = 'HELLO'
for letters in string:
    print(letters)
mohan Abdul
mohan Abdul
Pro Student 972 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 23,698 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-