Create a String With NewLine in Python

When I first started coding in Python more than a decade ago, one of the things that often confused me was how to properly add a new line inside a string.

Sometimes I wanted to display text neatly on multiple lines. Other times, I needed to format data for reports where each entry had to appear on a separate line.

In this tutorial, I’ll show you all the different ways you can create a string with new line characters in Python. I’ll also share some practical examples that I personally use in my projects.

Ways to Add New Lines in Python

Let me show you the scenario so you will understand where we can use these approaches. Suppose I need to take user input to collect their data like this,

Enter Your Name: Peter
Enter your age: 30
Enter your salary: 30000

After taking the user input, I need to show their data in this format,

Hello Peter 
 Your Current salary is $30000
 Your age is 30

Here, you can see the new lines in the result, so here, you will learn how we can give new lines in Python in different ways.

Method 1: Use Python’s \n Character

First, we will use \n character in Python, which is used to break the string and add a new line for the same string.

Syntax

str_name = " string 1 string 2 \n string 3
  • str_name = ” string 1 string 2 \n string 3: If we print this string name, then it will display string 1 and string 2 together and string 3 will print on new line.

Let’s understand how we can use the \n character in Python with a real-based scenario.

name  = input("Enter Your Name: ")
age  = input("Enter your age: ")
salary = input("Enter your salary: ")

print(f"\nThanks For Registration: \n\n Hello {name} \n Your Current salary is ${salary}\n Your age is {age}")

You can refer to the screenshot below to see the output.

Python New Line in String using new line Character

In the above code, we are using \n character to get a new line in a single string in Python. We’ve also given \n character double time so that it will skip two lines, and then it will start with this string, Hello {name}.

Method 2: Use print() Method in Python

Here, we will use the print() method to get a new line in Python. Generally, we use this method to display anything on the terminal. Every time you use the print() statement, it will display on a New line.

name = "John"
email = "[email protected]"
contact = "+1 xxxxxx89"

print("Registration Successfull: ")

print()

print("Your Name is: ",name)
print("Your Email is: ",email)
print("Your Contact is: ",contact)

You can refer to the screenshot below to see the output.

Python String Append New Line Using print() Method

In the above code, we’ve created variables called name, email, and contact number, assigned some static values to them, like this: name = “John”, and used an empty print() statement to insert a new line.

Method 3: Use Doc String (Triple Quotes) in Python

We can also use Docstring in Python to get the new lines in Python. Doc string in Python is a direct way to get the required format to print the string.

There are two uses of Docstrings in Python. It can be useful for giving multiline comments using triple quotes. If you initialize it inside a variable, it can act like a string element.

Syntax

var_name = ''' String 1 
String2 
String3'''
  • It will print in the same format that you will assign in the doc string.
cities = """New York
Los Angeles
Chicago
Houston
Phoenix"""

print(cities)

You can refer to the screenshot below to see the output.

New Line in Python using Doc string (Triple Quotes)

In the above code, we are using a docstring and printing city names on a new line. Then printing cities in a new line. So whatever you write in the triple quotes, it prints as it is.

Method 4: Use join() Method in Python

Here, we will use the join() method to get the string data in a New line in Python. We will also use the split() method to separate the string by a comma. Then, the join() method will concatenate that element into the new empty string with new lines.

Syntax

var_name = "\n".join(string)

Let’s see how the join() method will work to get a new line with a practical example in Python.

data = "New York, Los Angeles, Chicago, Phoenix, Houston".split(", ")

cities = "\n".join(data)

print(cities)

You can refer to the screenshot below to see the output.

Python String join Newline Using join() Method

In the above code, we have data that contains city names separated by commas in the string. We need to give a new line in place of commas.

First, we use the split() method to create a list of all elements separated by a comma. Then, we use the join() method with “n” in the new string, like this: “cities = “n”.join(data)”, which will concatenate all the list elements, including n, with every element.

Best Practices for Using New Lines in Python Strings

Over the years, I’ve learned a few best practices for handling new lines in Python:

  • Use \n when you need quick and simple line breaks.
  • Use triple quotes for longer multi-line text.
  • Use join() when combining lists into a string.
  • Always test your output if you’re writing to files.

Following these practices will make your Python code cleaner and easier to maintain.

When I look back at my early days of Python development, I realize how often I struggled with formatting strings.

Now, with these simple methods, I can easily create strings with new lines in Python for any situation, whether it’s printing receipts, generating reports, or writing logs.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.