Python new line

“Python new line” as the name indicates it is about adding a new line in the python program. In this article, we will discuss: What do we mean by python’s new line? How can we …

Python new line

Python new line” as the name indicates it is about adding a new line in the python program.

In this article, we will discuss:

  • What do we mean by python’s new line?
  • How can we add a new line in python?
  • Examples to add a new line in python.

What do we mean by python’s new line?

We mean by python new line if we are writing a piece of code, but we don’t want to print it out on the same line.

As for better understanding and better readability, we use a line break in all programming languages.

In python’s new line, we will discuss how we can create a line break in python.

How can we add a new line in python?

It is effortless than you think; you can easily add a new line in python by just writing backslash “\” and character “n”

Like: “/n”

Before saying anything else, I want to show you an example of how it works.

Examples of different methods to add a new line in python.

Example1 (Print statement):

print("Hello\nDrastic Family")

Output :

Python new line

As you know, whenever we write something in a print statement, it will print the output on the same line.

And as you can see by in above example.

Let’s take another example of a string.

Example 2 (String):

str='''Greetings!! \n To learn python new list \n you are welcome at Drastic code'''
print(str)

Output:

Python new line

You can see the example2 also added “\n” in-between string, and when we print the string, it shows output in multiple lines rather than a single.

Let’s see one more example to know further how we can add a new line with the .format method :

Example 3 (.format):

print("One{n1}"   "Two{n1}"    "Three".format(n1="\n"))

Output:

Python new line

As you can see, I did not add “\n” at the end of each statement; rather than putting it after every statement, I prefer it with the .format method.

Now, let’s check how we can add a new line in python with the .join method.

Example 4 (.join):

lst = ['One','Two','Three','Four']
lst = '\n'.join(lst)
print(lst)

Output:

Python new line

Conclusion :

“Python new line” means adding a new line in the python block of code or printing output on different lines with a line break.

To add a new line in python, “\n” is used after the end of the statement.

In this article, we have discussed strings can add a new line in python without writing it after every statement with the help of the .format and .join method.

Suggested Read: Remove Brackets from list Python

Leave a Comment