How to print new line in python
In Python, you can print a new line using the following methods:
-
print()
function with a newline character:print("Hello", end="\n")
The
end
parameter is used to specify the character to print after the output. By default, it's a space (" "
). By setting it to"\n"
, you're telling Python to print a newline character. -
print()
function with a newline string:print("Hello\n")
You can simply print a string that contains a newline character (
"\n"
). -
print()
function with a separateprint()
statement:print("Hello") print()
You can print a statement, followed by an empty
print()
statement, which will print a newline character. -
Using the
os
module:import os os.system("echo Hello\n")
This method is less common and is used when you need to print a newline character in a specific context, such as when working with shell commands.
-
Using the
print()
function with a separator:print("Hello", "World", sep="\n")
The
sep
parameter is used to specify the separator between the output. By setting it to"\n"
, you're telling Python to print a newline character between the output.
Remember that in Python 3.x, the print()
function is a function, whereas in Python 2.x, it's a statement.