输入:
a = input("enter-")
b = input("enter-")
c = input("enter-")
print(a, "\n", b, "\n", c, "\n")
输出:
enter-line1
enter-line2
enter-line3
line1
line2
line3
如何删除
line2
和line3
之前的空格?输入:
a = input("enter-")
b = input("enter-")
c = input("enter-")
print(a, "\n", b, "\n", c, "\n")
输出:
enter-line1
enter-line2
enter-line3
line1
line2
line3
line2
和line3
之前的空格?你可以使用print函数的参数sep。默认情况下,分隔符为一个空格:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
>>> print(a, b, c, sep='\n')
line1
line2
line3
print()
函数有比字符串更多的参数,其语法如下:
print(object(s), sep=separator, end=end, file=file, flush=flush)
默认分隔符是' '
,因此每个元素之间都会添加空格。
要打印时不带空格,需要像这样使用
print(a,"\n",b,"\n",c,"\n", sep='')
object(s)
? - loa_in_使用Python中的print
语句,并在它们之间加上逗号,自然会产生空格。例如,如果我使用了print(a,b,c)
,那么输出结果将是line1 line2 line3
。这里,通过使用print(a,"\n",b,"\n",c,"\n")
,您在换行后放置了一个空格。因此,要么使用单独的打印语句,要么使用"\n"
作为参数之间的分隔符而不使用逗号。
print
函数默认使用空格分隔参数。消除空格的几种方法之一是将单个字符串传递给print
,例如:print("\n".join([a, b, c]))
。 - Timur Shtatland