Print empty line?

后端 未结 7 873
青春惊慌失措
青春惊慌失措 2020-12-25 12:37

I am following a beginners tutorial on Python, there is a small exercise where I have to add an extra function call and print a line between verses, this works fine if I pri

7条回答
  •  感动是毒
    2020-12-25 13:06

    Python's print function adds a newline character to its input. If you give it no input it will just print a newline character

    print()
    

    Will print an empty line. If you want to have an extra line after some text you're printing, you can a newline to your text

    my_str = "hello world"
    print(my_str + "\n")
    

    If you're doing this a lot, you can also tell print to add 2 newlines instead of just one by changing the end= parameter (by default end="\n")

    print("hello world", end="\n\n")
    

    But you probably don't need this last method, the two before are much clearer.

提交回复
热议问题