How to Use /n in Python With Examples (Print New Line)

In Python, the escape sequence \n represents a newline character. It is used to insert a line break or start a new line in a string. Here’s an example of how you can use it:

# Example 1: Printing multiple lines
print("Line 1\nLine 2\nLine 3")Code language: Python (python)

Output:

Line 1
Line 2
Line 3Code language: Python (python)

In this example, the \n escape sequence is used to create line breaks between “Line 1,” “Line 2,” and “Line 3” when they are printed.

# Example 2: Storing a string with newlines
message = "Hello,\nWelcome to Python programming.\nLet's get started!"
print(message)Code language: Python (python)

Output;

Hello,
Welcome to Python programming.
Let's get started!
Code language: Python (python)

In this case, the string message contains newline characters (\n) to separate different lines. When the string is printed, each line appears on a new line.

You can also use the newline character within triple quotes (''' or """) to define multi-line strings:

# Example 3: Multi-line string
text = '''This is a multi-line
string using triple quotes.
It can span across multiple lines.'''
print(text)Code language: Python (python)

Output:

This is a multi-line
string using triple quotes.
It can span across multiple lines.Code language: Python (python)

Here, the \n within the triple quotes creates line breaks in the output.

So, depending on your use case, you can use the \n escape sequence to insert newlines in Python strings.

How do you use \n between variables in Python?

To use the \n escape sequence between variables in Python, you need to concatenate the variables and the \n escape sequence within a string. Here’s an example:

# Example 1: Using \n between variables
name = "John"
age = 25

message = "Name: " + name + "\nAge: " + str(age)
print(message)
Code language: Python (python)

Output:

Name: John
Age: 25Code language: Python (python)

In this example, the variables name and age are concatenated with the string literals “Name: ” and “Age: “, respectively. The \n escape sequence is inserted between them to create a line break when the message string is printed.

If you’re using f-strings (formatted strings), you can include the \n escape sequence directly within the f-string. Here’s an example:

# Example 2: Using \n within an f-string
name = "John"
age = 25

message = f"Name: {name}\nAge: {age}"
print(message)
Code language: Python (python)

Output:

Name: John
Age: 25Code language: Python (python)

In this case, the variables name and age are enclosed within {} placeholders within the f-string. The \n escape sequence is included directly within the f-string, and it gets evaluated as a line break when the message string is printed.

So, by concatenating variables and the \n escape sequence within strings or using f-strings, you can achieve the desired newline effect between variables in Python.

Read More;

  • I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

    View all posts

Leave a Comment