Python is an excellent language to start coding as it has a clean syntax and easy-to-learn structures. The print function, which is a built-in function in Python, is an essential tool for displaying output on a console or in a program.
However, when we are printing output, we may want to skip a line to make the output more readable. In this tutorial, we will explore the different ways to skip a line in Python print.
Steps
1. The newline character
The easiest way to skip a line in Python print is by using the newline character “\n”. This character is used as a newline character in all programming languages. To use it in Python print, simply add “\n” at the end of the string that you want to print. For instance,
1 |
print("Hello, World!\n") |
This code will print “Hello, World!” on one line and then skip to the next line.
2. The end parameter
The print function has an optional parameter named “end” which specifies what character to use to separate the printed arguments. By default, the value of the “end” parameter is “\n”. To skip a line using the end parameter, set the value of “end” to an empty string. For example,
1 |
print("Hello, World!", end='')<br>print("This text will be on a new line.") |
This code will print “Hello, World!” on the same line as “This text will be on a new line.”
3. Using multiple print statements
Another way to skip a line in Python print is simply by using multiple print statements. Each print statement will print on a new line by default, so using multiple print statements will result in output that is separated by blank lines. For instance,
1 |
print("First line")<br>print()<br>print("Third line") |
This code will print “First line”, skip a line, and then print “Third line”.
Conclusion
We have shown different methods to skip a line in Python print, including the newline character, the end parameter, and multiple print statements. By using these methods, you can make your Python code output more readable and organized.
Full code
1 2 3 4 5 6 7 8 9 |
print("First line\n") print("Second line without newline character") print("This text will be on a new line.", end='') print("But this will be on the same line.") print() print("Multiple print statements can also be used.") print() print("This text will be on another new line.") |