Python 在一个语句中打印多行没有前导空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25676970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print multiple lines in one statement without leading spaces
提问by Dan Todorovic'
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.
因此,对于我的第一个项目,它是一个简单的程序,可以打印您所在班级的姓名以及您就读的高中。让我感到困惑的一件事是,对于其中一个,我必须使用一个print()语句来完成所有这些,并且我需要对其进行格式化,以便每条信息都在不同的行上。
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')
采纳答案by jwodder
printinserts 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_nameand last_nameor between course_idand course_name, etc. You could then go on to insert , ' 'manually where you need it in the printstatement, but by that point it might be simpler to just give printa single argument formed by concatenating the strings together with explicit spaces:
print在每个参数之间插入一个空格。您可以通过, sep=''在 last 之后添加来禁用它'\n',但是在first_nameand之间last_name或在course_idand之间不会有任何空格course_name,等等。然后您可以继续在语句中, ' '需要它的地方手动插入print,但到那时它可能是更简单地给出print通过将字符串与显式空格连接在一起形成的单个参数:
print(first_name + ' ' + last_name + '\n' + course_id + ' ' + course_name
+ ' ' email + '\n' + school + '\n')
回答by Celeo
I recommend reading through str.format()to print your information.
我建议通读str.format()以打印您的信息。
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 [email protected]
Red River Highschool
回答by rmunn
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 dowant separated. E.g.,
正如这里提到的,您可以使用函数的sep=''参数print()。这将允许您将打印值(默认为空格)之间的分隔符设置为您想要的任何内容,包括空字符串。你必须记住,你的价值观之间加空格不希望分离。例如,
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():
有一种更好的方法可以做到这一点,涉及format()字符串的方法,但您的教授可能会将其保存到下一课,所以我现在不会详细介绍它。如果需要更多详细信息,请访问 Python 文档的链接,并阅读有关格式字符串语法的部分。我将向您展示一个示例,说明您使用format()以下代码的样子:
print("{} {}\n{} {} {}\n{}".format(first_name, last_name, course_id, course_name, email, school))
Note no \nat the end, since print()automatically adds a newline unless you tell it otherwise.
注意最后没有\n,因为print()除非您另有说明,否则会自动添加换行符。
回答by Miyek
Just don't add space in your code
只是不要在代码中添加空格
print(first_name, last_name, '\n',course_id, course_name, email, '\n', school, '\n')
回答by Jyothirmai Veluru
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
输出:: 7.068577499999999 1.5 9.4247699999999999
the above you will get corrcet answer for this code.
上面你会得到这个代码的正确答案。
回答by Swati Srivastava
There are a number of ways to do so. First can be str.format() as
有多种方法可以做到这一点。首先可以是 str.format() 作为
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)

