Print multiple lines in one statement without leading spaces

后端 未结 6 1439
有刺的猬
有刺的猬 2021-01-13 03:00

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 the

相关标签:
6条回答
  • 2021-01-13 03:27

    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
    
    0 讨论(0)
  • 2021-01-13 03:34

    Just don't add space in your code

    print(first_name, last_name, '\n',course_id, course_name, email, '\n', school, '\n')
    
    0 讨论(0)
  • 2021-01-13 03:48

    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.

    0 讨论(0)
  • 2021-01-13 03:48

    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)
    
    0 讨论(0)
  • 2021-01-13 03:49

    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')
    
    0 讨论(0)
  • 2021-01-13 03:50

    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.

    0 讨论(0)
提交回复
热议问题