Add new line python print
In Python, you can add a new line to the output of a print
statement using the following methods:
-
Using the
\n
character:print("Hello", "\nWorld")
This will output:
Hello World
The
\n
character is an escape sequence that represents a new line. -
Using the
print()
function with multiple arguments:print("Hello") print("World")
This will output:
Hello World
Each
print()
statement will start on a new line. -
Using the
print()
function with a string and a newline character:print("Hello" + "\nWorld")
This will output:
Hello World
The
+
operator is used to concatenate the string and the newline character. -
Using the
print()
function with a formatted string:print(f"Hello\nWorld")
This will output:
Hello World
The
f
string prefix is used to create a formatted string, and the\n
character is used to insert a new line. -
Using the
print()
function with a list of strings:print(["Hello", "World"])
This will output:
Hello World
Each element in the list is printed on a new line.
Note that in Python 3.x, the print()
function is a function, whereas in Python 2.x, it is a statement. In Python 2.x, you can use the print
statement with multiple arguments, like this:
print "Hello", "World"
This will also output:
Hello
World