![]() |
|
[Tut] How to Skip a Line in Python using \n? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Skip a Line in Python using \n? (/thread-99667.html) |
[Tut] How to Skip a Line in Python using \n? - xSicKxBot - 07-05-2022 How to Skip a Line in Python using \n? 5/5 – (1 vote) ![]() Skip Line \n Summary:
Python’s newline character to indicate the end of a line of text is Code: \nIf you print a string to the shell using the built-in Code: print()Code: \nCode: 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 Code: forCode: #################################
# 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
ContentYou can customize this behavior of separating two lines using a single newline character Code: '\n'Code: end='\n'Code: print()For example, you can skip two lines in Python using Code: print(my_string, end='\n\n')Code: '\n\n'Code: 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 OutputAnother way to skip a line in the Python output is to add an empty Code: print()Code: with open('my_filename.txt', 'r') as my_file: for line in my_file.readlines(): print(line) print() # Output:
My File Content # End Output |