Skip Line \n
Summary:
- Python’s newline character
\n
indicates the end of a line of text. - The built-in
print()
function automatically adds a newline character\n
at the end. - You can customize this behavior of separating two lines using a single newline character
'\n'
by changing the defaultend='\n'
argument of theprint()
function to your desired string. - Another way to skip a line in the Python output is to add an empty
print()
statement that will just print an empty line and do nothing else.
Python’s newline character to indicate the end of a line of text is \n
.
If you print a string to the shell using the built-in print()
function, Python automatically adds a newline character \n
at the end.
PYTHON CODE:print('hello\nworld\n\nPython is great!')
OUTPUT:hello world Python is great!
For example, if you iterate over the text in a file using a for
loop and print each line in the loop body, the lines are separated with single new lines.
################################# # File: my_filename.txt # ################################# # My # # File # # Content # ################################# with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) # Output: My File Content
You can customize this behavior of separating two lines using a single newline character '\n'
by changing the default end='\n'
argument of the print()
function to your desired string.
For example, you can skip two lines in Python using print(my_string, end='\n\n')
by chaining two newline characters '\n\n'
.
with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line, end='\n\n') # Output: My File Content # End Output
Another way to skip a line in the Python output is to add an empty print()
statement that will just print an empty line and do nothing else.
with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) print() # Output: My File Content # End Output