7

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 = 'blah@gmail.com'
school= 'Red River Highschool'

#variables printed for B
print(first_name, last_name, '\n', course_id, course_name, email, '\n', school, '\n')
Jason S
  • 13,538
  • 2
  • 37
  • 42
Dan Todorovic'
  • 67
  • 1
  • 1
  • 5

7 Answers7

5

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')
jwodder
  • 54,758
  • 12
  • 108
  • 124
4

As mentioned here, you can use the sep='' argument to the print() function. That will let you set the separator between printed values (which is a space by default) to whatever you want, including an empty string. You'll have to remember to add spaces between the values that you do want separated. E.g.,

print(first_name, ' ', last_name, '\n', course_id, [...], sep='')

There's a better way to do this, involving the format() method on strings, but your professor is probably saving that one for the next lesson so I won't cover it in detail now. Follow the link to the Python docs, and read the section on Format String Syntax, if you want more details. I'll just show you an example of what your code would look like using format():

print("{} {}\n{} {} {}\n{}".format(first_name, last_name, course_id, course_name, email, school))

Note no \n at the end, since print() automatically adds a newline unless you tell it otherwise.

Community
  • 1
  • 1
rmunn
  • 34,942
  • 10
  • 74
  • 105
1

I recommend reading through str.format() to print your information.

The spaces in your output come from the fact that you've called the print function, passing a list of strings, instead of passing the print function a single string.

print(first_name + ' ' + last_name + '\n' + course_id + ' ' + course_name + ' ' + email + '\n' + school)

yields

Daniel Rust
Csci 160 Computer Science 160 blah@gmail.com
Red River Highschool
Celeo
  • 5,583
  • 8
  • 39
  • 41
  • https://docs.python.org/3.3/library/stdtypes.html#str.format would be a better link for him since he appears to be using Python 3. – rmunn Sep 05 '14 at 00:45
  • I don't like this method because if any of the variables happen to be non-string you will run into problems. – SethMMorton Sep 05 '14 at 00:52
0

Just don't add space in your code

print(first_name, last_name, '\n',course_id, course_name, email, '\n', school, '\n')
Miyek
  • 1
  • 2
0

There are a number of ways to do so. First can be str.format() as

print ('{} {}\n{} {} {}\n{}'.format(first_name, last_name, course_id, course_name, email, school))

Second can be

print (first_name, ' ', last_name, '\n',course_id, ' ', course_name, ' ', email, '\n',school, sep = '')

And the third can be

print (first_name + ' ' + last_name + '\n' + str(course_id) + ' ' + course_name + ' ' + email + '\n' + school)
Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18
0

Simple multiline print in python using f-string,

first_name = 'Daniel'
last_name = 'Rust'
course_id = 'Csci 160'
course_name = 'Computer Science 160'
email = 'blah@gmail.com'
school= 'Red River Highschool'

#variables printed for B
print(first_name, last_name, '\n', course_id, course_name, email, '\n', school, '\n')

print(f''' 
{first_name}, {last_name}
{course_id}, {course_name}, {email}
{school}''')

Daniel Rust
 Csci 160 Computer Science 160 blah@gmail.com
 Red River Highschool


Daniel, Rust
Csci 160, Computer Science 160, blah@gmail.com
Red River Highschool

[Program finished] 
Subham
  • 397
  • 1
  • 6
  • 14
-1

How to print several lines by using one print statement and how to add new line?

pi = 3.14159 # approximate
diameter = 3
radius = diameter/2
area = pi * radius * radius
circumference = 2 * pi * radius
print("{}\n{}\n{}\n".format(area,radius,circumference))

output:: 7.068577499999999 1.5 9.424769999999999

the above you will get corrcet answer for this code.