Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print multiple lines in one statement without leading spaces

So for my first project it is a simple program that prints your name class you are in and what high school you went to. The one thing that is messing me up is for one of them I have to use one print() statement for all this and I need to format it so that each piece of information is on a different line.

What I want for the format:

first_name, last_name
course_id, course_name, email
school

But what I get is

first_name, last_name
 course_id, course_name, email
 school

How do I remove the space?

My code is as follows:

first_name = 'Daniel'
last_name = 'Rust'
course_id = 'Csci 160'
course_name = 'Computer Science 160'
email = '[email protected]'
school= 'Red River Highschool'

#variables printed for B
print(first_name, last_name, '\n', course_id, course_name, email, '\n', school, '\n')
like image 444
Dan Todorovic' Avatar asked Sep 05 '14 00:09

Dan Todorovic'


1 Answers

print inserts a space between each argument. You can disable this by adding , sep='' after the last '\n', but then there won't be any spaces between first_name and last_name or between course_id and course_name, etc. You could then go on to insert , ' ' manually where you need it in the print statement, but by that point it might be simpler to just give print a single argument formed by concatenating the strings together with explicit spaces:

print(first_name + ' ' + last_name + '\n' + course_id + ' ' + course_name
      + ' ' email + '\n' + school + '\n')
like image 63
jwodder Avatar answered Sep 20 '22 17:09

jwodder