简体   繁体   English

Python:如何在打印时换行?

[英]Python: How can I make a new line when printing?

I am using Cmd, PIL, and Image to make a program called Visual Object Creator, aka VOC. 我正在使用Cmd,PIL和Image制作一个名为Visual Object Creator的程序,又名VOC。 I made a command prompt and when you type in a command, let's say, bluesquare, it creates just that. 我创建了一个命令提示符,当您键入命令时,比如说bluesquare,它就是这样创建的。 But when I do, it makes the image right in front of my command prompt. 但是,当我这样做时,它将图像显示在命令提示符之前。 It looks terribly ugly. 看起来很难看。 It makes me type the command in the next line, away from the (Cmd) prompt. 它使我在(Cmd)提示下的下一行中键入命令。 It's hard to explain, just use trinket.io and see my problem, because that's the only compiler I find that works 很难解释,仅使用trinket.io并查看我的问题,因为那是我发现唯一可行的编译器

I tried using /n and printing simply a blank line, but it never worked. 我尝试使用/ n并仅打印空白行,但从未成功。 I have seen the /n being used but it simply prints the actual /n and not the blank line. 我已经看到了/ n的使用,但是它只是打印实际的/ n而不是空白行。 I am sorry I pasted the whole file, but every image creating command that I programmed does it! 很抱歉,我粘贴了整个文件,但是我编写的每个图像创建命令都执行了它!

from PIL import Image
from cmd import Cmd
class Root(Cmd):
  intro = "Visual Object Creator v.04."
  prompt = ">"
  def do_redsquare(self, inp):
    print("VOC created this image for you! rsqr.jpg")
    rs = Image.new("RGB", (50, 50), color = "red")
    rs.save("rsqr.jpg")
  def help_redsquare(self):
    print("VOC creates a red square under the filename rsqr.jpg.")
  def do_exit(self, inp):
    return True
  def help_exit(self):
    print("Ends the VOC application.")
  def do_greensquare(self, inp):
    print("VOC created this image for you! gsqr.jpg")
    gs = Image.new("RGB", (50, 50), color = "green")
    gs.save("gsqr.jpg")
  def help_greensquare(self):
    print("VOC creates a green sqare under the filename gsqr.jpg.")
  def do_bluesquare(self, inp):
    print("VOC created this image for you! bsqr.jpg")
    bs = Image.new("RGB", (50, 50), color = "blue")
    bs.save("bsqr.jpg")
  def help_bluesquare(self):
    print("VOC creates a blue square under the filename bsqr.jpg")
Root().cmdloop()

There were no errors, but it was making me enter commands on a blank line when I actually want to do it on the line of the (cmd) prompt. 没有错误,但是当我实际上想要在(cmd)提示行上输入命令时,这使我在空白行上输入命令。

The correct newline character for python is \\n . python的正确换行符是\\n

The \\ (backslash) is used to start an "escape sequence" which has special meaning to Python. \\ (反斜杠)用于启动“转义序列”,这对Python具有特殊的意义。 Some of the options are: 一些选项是:

\\n newline \\n换行符

\\t tab \\t标签

\\\\ backslash \\\\反斜杠

print() should print a blank line. print()应该打印一个空行。 Example: 例:

print('line 1')
print()
print('line 2')

will output: 将输出:

line 1

line 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM