How to Print New Line in Python (or Print in Next Line)

Hi there, In this tutorial blog, we will learn how to print a new line in Python. You will learn 6 different ways to print in the next line so that you can understand the topic completely. Well, we use \n to print a new line in most programming languages like Python, C, C++, Java, and so on. The \n is known as the newline character. We have covered all possible uses of newline character in python programming.

In the first Five Examples, you will learn how to break lines and print things in the next line (using /n) in python programming, and in the 6th example, you will learn about multiline strings.

Print newline or print in the next line in Python
Print New Line or print in next line in Python

Print in Newline Using Newline Character in String

As we already have mentioned that there is a new line character that we can use for changing the line when you are printing sting.

The newline character in Python-

\n

When to use the newline character \n?

I would say, mostly when you are printing text or string and you want that the message that you are printing should look organized and clean.

Well, let’s have a look at the simple Python program in which we are printing some text messages and we have used \n newline character to slip the text in lines for better readability.

print("He there, My name is Jack\n I'm from United States of America")

If you run the code in the example then you will get the output like this-

Print Numbers in New line in Python

It’s also possible to print numbers in the new line when working with numbers in Python programming. For this purpose also, we will use \n which is known as newline character.

As we learned that while working with string we place \n where we want to break the line and print the further messages in the next line. When we work with string we keep the text between double or single quotation marks and \n can also only be placed between the double or single quotation marks otherwise the Python interpreter will print an error.

So what if we want to print something like this-

23
29
31
65

Note: These numbers are not in any sequence. These are just random numbers that are printed on different lines.

Can you guess how could we do so?

Well, let me tell you that we can do so by placing the newline character in quotation marks (“\n”) after each number.

Let’s have an example-

a = 23
b = 29
c = 31
d = 65
print(a,"\n", b,"\n", c,"\n",d)

Note: If you try to so do then do not forget to separate the variables and strings using a comma.

In the given example, we have used variables to store the value so that we can easily use the newline character with the print function. If you are completely new to python programming and don’t know about the variables then please go through the next section in which we have covered print in the next line while using variables.

If we run this code we will get output like this-

In the example, we took only the Integer numbers but you can do the same with any other type of number.

Print a New Line While Using Variables

I assume you already know about variables in Python programming. So, in this tutorial, I am not explaining variables. But however, I would like to mention that variables in programming languages work like containers. You can assign any sort of value to the variable.

An example of a Variable

Name = "Jack"
Age = 26
Weight = 72.8

In the above example, we have defined three variables Name, Age, and Weight and we have assigned some values to them. The Name variable contains string value, the Age variable contains Integer value, and Weight contains float value.

Now, let’s have an example of printing things in a new line while we use variables to store values.

Name = "Jack"
Age = 26
Weight = 72.8
print(Name,"\n",Age,"\n",Weight)

Can you guess what will be the output?

Well, if we run the code which is in the example then we will get the output like this-

Jack
 26
 72.8

Create a Variable for New Line

Do you know that you can make your jobs easier when you are working with variables? Well, let me tell you that you can create a variable and use it for changing the line.

In order to do so, create a variable and assign “\n” as a value to it.

newline = "\n"

Let’s understand with an example-

Name = "Jack"
Age = 26
Weight = 72.8
newline = "\n"
print(Name, newline, Age, newline, Weight)

If you look at the example, In the print method we have used newline variable itself to print the value of the next variable in a new line.

Output-

The format() Method for Printing New Line in Python

You may be surprised to know that we can use the format() method also to print strings or numbers in a new line. But, yes again we need a variable containing “\n” (New Line Character).

The format method is used for formatting the specified string value and inserting them inside the placeholder. In order to specify place holder inside the string, we use curly brackets.

Let’s have an example of printing string in a new line using the format() method-

newline = "\n"
print("Hey I am Jack {} And I am from USA". format(newline))

If you are the given code you will get output like this-

Hey I am Jack 
 And I am from USA

Print String in Multiple Lines in Python

You can define a variable “multiline string variable” to print strings in multiple lines in python programming. In order to define a variable multiline string, we have to use three times double or single quotations. Let’s have a look at the example-

String = """Hi there, I am Jack from USA
I live in florida
and I love computer programming """

print(String)

In the above example, we have created a multiline string variable that contains multiple lines of string values. And furthermore, we have used the print() method to print the value of the String variable.

Conclusion

In this tutorial blog, we shared 6 different ways to print new lines in python programming. Here simply means, how to print the output in a new line or we can also say how to break lines. Also, we have covered how you can create a variable that can store the string values in multiple lines. We explain almost all the possible ways to use newline character (/n) to print things in the next line.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top