Print multiple lines in one statement without leading spaces

后端 未结 6 1443
有刺的猬
有刺的猬 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: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.

提交回复
热议问题