How to print on new line in python
In Python, you can print on a new line using the \n
character. Here are a few ways to do it:
-
Using
\n
:print("Hello") print("\nWorld")
This will print "Hello" on one line and "World" on the next line.
-
Using
print()
with multiple arguments:print("Hello", "World")
This will print "Hello" and "World" on the same line, separated by a space. If you want to print them on separate lines, you can use the
sep
argument:print("Hello", "World", sep="\n")
-
Using
print()
with a string and a newline character:print("Hello" + "\n" + "World")
This is similar to the first example, but uses string concatenation instead of separate
print()
statements. -
Using
print()
with a list of strings:print(["Hello", "World"])
This will print each string in the list on a new line.
-
Using
print()
with a formatted string:print(f"Hello\nWorld")
This uses an f-string to insert the newline character into the string.
Note that in Python 2.x, you need to use print
with parentheses, like this: print ("Hello\nWorld")
. In Python 3.x, you can use print
without parentheses, like this: print("Hello\nWorld")
.