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 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:
Alex
 5

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
MOD
Chris Freeman
Treehouse Moderator 68,457 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.