Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Add a newline character in Python – 6 Easy Ways!

Addition Of Python Newline Character

Hey folks! Hope you all are doing well. In this article, we will be unveiling Different ways to add a newline character in Python(\n) to the output of the data to be printed.

So, let us get started!


Technique 1: Add a newline character in a multiline string

Python Multiline String provides an efficient way to represent multiple strings in an aligned manner. A newline(\n) character can be added to multiline string as shown below–

Syntax:

string = '''str1\nstr2....\nstrN'''

We can easily use ‘\n’ before every string which we want to display on a new line in a multiline string.

Example:

str='''Hello all!! \nI am Pythoner \nWelcome to the AskPython Tutorial''' 
print(str)

Output:

Hello all!! 
I am Pythoner 
Welcome to the AskPython Tutorial

Technique 2: Addition of newline to a Python List

Python List can be considered as a dynamic array that stores heterogeneous elements into it at dynamic runtime.

The string.join() function can be used to add a newline amidst the elements of the list as shown below–

Syntax:

'\n'.join(list)

Example:

lst = ['Python','Java','Kotlin','Cpp']
print("List before adding newline character to it:",lst)
lst = '\n'.join(lst)
print("List after adding newline character to it:\n",lst)

Output:

List before adding newline character to it: ['Python', 'Java', 'Kotlin', 'Cpp']
List after adding newline character to it:
 Python
Java
Kotlin
Cpp

Technique 3: Displaying a newline on the console

At the very beginning stage, it is important to know the execution of functions on the console. To add a newline on the console use the below code–

print("str1\nstr2")

Example:

Python newline with console Add a newline character in Python
Python newline with console

Technique 4: Displaying a new line through print statement

The newline character can be added to print() function in order to display the string on a new line as shown below–

Syntax:

print("str1\nstr2\n...\strN")

Example:

print("Hello Folks! Let us start learning.")
print("Statement after adding newline through print() function....")
print("Hello Folks!\nLet us start learning.")

Output:

Hello Folks! Let us start learning.
Statement after adding newline through print() function....
Hello Folks!
Let us start learning.

Technique 5: Add a newline character through Python f-string

Python f-string also represents the string statements in a formatted manner on the console. To add a newline character through a f-string, follow the below syntax:

newline = '\n'
string = f"str1{newline}str2"

Example:

newline = '\n'
str = f"Python{newline}Java{newline}Cpp"
print(str)

Output:

Python
Java
Cpp

Technique 6: Writing a new line to a file

A newline character can be appended to a Python file using the below syntax:

Syntax:

file_object.write("\n")

Example:

Here, we have used Python.txt file with the below predefined content as shown–

Python Text-file
Python Text-file
import os 
file = "/Python.txt" 
with open(file, 'a') as file: 
  file.write("\n")  

Output:

As seen below, a new line gets added to the file content.

Add newline To A Python File
Add newline To A Python File

Conclusion

By this, we have come to the end of this topic. Feel free to comment below incase you come across any question.

Till then, Happy Learning!!


References