我如何专门将\ n用于打印功能?

时间:2019-10-02 18:57:26

标签: python printing newline

a = int(input("Enter a number: "))
print("The next number for the number", a, "is", a+1)
print("The previous number for the number", a, "is", a-1)

我只想在打印功能中使用单行命令,我说要分两行打印。我尝试了以下代码,但是由于它在空格后打印第二行,因此无法正常工作。我想删除这个无用的空间。

print("The next number for the number", a, "is", a+1,"\n","The previous number for the number", a, "is", a-1)

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

print("The next number for the number %i \nThe next number for the number %i" % (int(a)+1, int(a)-1))

答案 1 :(得分:0)

无需将\ n单独连接。它将在字符串中的任意位置创建换行符:

print(“数字的下一个数字”,a,“是”,a + 1,“ \ n数字的前一个数字”,a,“ is”,a-1)

数字3的下一个数字是4 数字3的前一个数字是2