How to Add a Newline Character in Python?

To increase the readability of text/document and source code files the special character named “newline” is used in Python. There are multiple symbols or sequences of symbols used in a different programming language to represent the newline character. In Python, different methods such as “\n” newline character, multiline strings, os.linesep, etc are used to add/insert a newline character.

This post provides various ways to add a newline character in Python using numerous examples:

Method 1: Using “\n” Newline Character

The “\n” is a newline escape character in Python. The escape characters are initialized in the program using the “\” backslash followed by the specific character value. The “\n” is utilized to add/insert a new line in Python. Let’s add a newline character in Python using various examples:

Example 1: Add Newline Character to String

The below code uses the newline character “\n” to add newline in the input string:

Code:

string_value = "Python\nGuide"
print(string_value)

The “\n” newline character is added between the string such as “Python\nGuide”.

Output:

The new line has been added.

Example 2: Add Newline Character to List

The below code is used to add a newline character to the list:

Code:

list_value = ['Python', 'Linux', 'Ubuntu']
print(list_value, '\n')
list_value = '\n'.join(list_value)
print(list_value)
  • The list is initialized in the program named “list_value”.
  • The “join()” method accepts the list as an argument and adds it to the string containing the “\n” newline character. 
  • The Newline is added to each joined element of the list.

Output:

The Newline has been added to the list.

Example 3: Adding Newline Character Using f-string Method

The “f-string” method is used for formatting the string in Python. The below code uses the “f-string” method to add a newline character:

Code:

new_line = '\n'
output = f"Python{new_line}Guide"
print(output)
  • The “\n” newline character is assigned to a variable named “new_line”.
  • The “f-string” method placed the newline character inside the string using the “{ }” string placeholder. 

Output:

The new line has been added.

Example 4: Adding a Newline Character to a Text File

The following code is used to add a newline character to a text file:

The text file contains the following content:

Code:

with open('example.txt', 'a') as f:
  f.write("\nLinux Guide")
  • The “open()” function is used to open the file named “example.txt” in appending mode “a”.
  • The “f.write()” function adds the newline character along with the text.

Output:

The new line has been added to the text.

Method 2: Using Multiline Strings

The multiline strings are initialized using three double or single quotes in Python. The multiline strings are used to add new lines along with the text. Here is an example code:

Code:

string_value = """Python
Guide by
itslinuxfoss
"""
print(string_value)
  • The multiline string named “string_value” is initialized in the program.
  • The specific string is added to the new line.

Output:

The new line has been added.

Method 3: Using os.linesep

The “os.linesep” attribute of the “os” module is used in the below code to add a newline:

Code:

import os
value = 'Python'+ os.linesep + 'Guide'
print(value)

The module named “os” is imported and the “os.linesep” is used to add the new line between the two string values.

Output:

The new line has been added.

How to Remove a Newline in Python?

In Python, the lstrip()“ function is utilized to clear the leading spaces from the input string. To remove a newline, the “lstrip()” function is used in Python:

Code:

string_value = "\nPython Guide"
print(string_value)
print(string_value.lstrip())
  • The string with the newline character is initialized in the program.
  • The “lstrip()” function removes the newline character from the given string.

Output:

The new line has been removed.

Conclusion

To add a newline character, the “\n” escape character, the “multiline strings” and “os.linesep” attribute of the os module is utilized in Python. To add a new line to a string or list the type of escape character “\n” is used in Python. The “f-string” method can be used to add the “\n” character to a string. The “f.write()” function accepts the “\n” newline character along with the text as an argument to append the content with a newline. The multiline string and os.linesep attribute is used to add newlines in Python. 

This guide presented various methods to add a newline character in Python using appropriate examples.