Important Python Concept

What does \n do in python?

Hey there!
In this article, we’ll be learning what does \n do in python.


New Line Character


Basically, the backslash n is used to specify the end of one line and the starting of the next.
Or let’s just frame it in an easy statement.
The \n is just like a full stop i.e ending of one line and
beginning of the next. Pretty easy right?

Basic Overview


The \n is also known as the new line character.
The new line character includes:
1.The letter n
2.The backslash (\)

Default Behavior

By default, python prints different print statements in different lines because \n has been added to each line itself.
Now, let’s take an example
print("hey there")
print("hey there")
view raw hosted with ❤ by GitHub
hey there
hey there

This is because, by default, \n has been added to each line by Python.


Use Case of \n in Python


So to add a newline in python, all you need is \n.
Now let’s understand this with an example:


print("hey \nthere")
view raw hosted with ❤ by GitHub



Here, inserting \n will lead to the creation of a new line i.e “hey” in one line and “there” in eventually the other.
So by default, Python uses the \n after the end of the whole string.
But we can always use \n as per our requirement.


End Parameter in Python


To change this default behavior, we can add an end parameter to the print statement.
Demonstration through an example:


print("hey,"end=" ")
view raw hosted with ❤ by GitHub
hey there

So, by using the end parameter we could print “hey” and “there” collectively in one line as per our use case.



So in simple words, need a new line?
Use backslash n!


In this python tutorial, we learned what does \n do in python.
I hope this article helped you.
In case the solution is not clear to you, I suggest you take a live 1:1 lesson on this subject from me. I am an Oracle-certified tutor who would love to teach this stuff to you.
Aniket Malik
Aniket Malik

Aniket Malik


Facing difficulty with

this concept?