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 Collections (Retired) Dungeon Game Building the Game: Part 2

igsm '
igsm '
10,440 Points

Dungeon PART 2. What does end = '' mean?

Hey! Please can someone explain once again what does end ='' mean and how can I write this print statement (print tile.format('X'), end='') in Python 2, as I do not use parentheses in print statements in Python 2 and if I do, it give out Syntax Error... Thanks

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

In Python 2, you'd do it like:

print "something",

And it would continue to print on the same line because of that comma.

In the context of the drawing the game map, the two aren't quite the same. ending python2's print with comma adds a space

Examples:

#python3
for row in range(3):
    for col in range(3):
        print('.', end='')
    print() #end line
#output:
...
...
...

#python2 (from script, not interactive)
for row in range(3):
    for col in range(3):
        print '.',
    print #end line
#output:
. . .
. . .
. . .

#python2 alternative to mimic python3 print(end='')
myString = ''
for row in range(3):
    for col in range(3):
        myString = myString + '.'   
    myString = myString + '\n' #newline char
print myString
#(this works when in a script, 
# but not interactively for me)
#output:
...
...
...

#variation: replace myString with a list.
#Call .append('.') each cycle,
#then afterwards print ''.join(listName)

he did explain it. if you dont say end="" then print statement by default adds a new line character to the end. but here you dont wnat that so you are telling python to not do that

Do it how it is done in the video. Just import this (from __future__ import print_function) at the top of your file. This will treat your prints as a python 3.x print.