Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Comparison Solution

Alex Kravchenko
Alex Kravchenko
1,278 Points

Printing new line with variables only, syntax error or always has a space on before the second var, WHY?

Some very frustrating that I ran into here. I tried to do the print users name and number on separate lines with variables only, for example.

name = 'Alex'
number = 5

So I want to do: --print(name, number)-- but with a line break.

print(name, \nnumber)-- error
print(name, '\n', number)-- prints:

How do I get it to print without the space before 5? I have only been able to do this if I did :

print('{} \n{}'.format(name, number))

But it seems unnecessary to do a .format there.

[MOD: added ```python formatting -cf]

2 Answers

Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

The built-in function print:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

has a sep parameter that controls what is printed between objects. You could try:

print(name, \n, number, sep=‘’)

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

Alex Kravchenko
Alex Kravchenko
1,278 Points

Thank you so much Chris. I will study up on the link you provided. I really appreciate the answer.