In this tutorial, you’ll learn how to use the Python new line character and how to print without newlines. Being able to control how your program prints strings is an important skill, especially when working with text files or command-line interfaces (CLIs). In this tutorial, you’ll learn different ways to include a newline character in your Python program, including the
\n character and multi-line strings.
Being able to print across new lines increases the readability of your program’s output. Similarly, newline characters are often found in text files. Being able to understand how these characters work in Python will allow you to create programs that respond how you expect them to.
By the end of this tutorial, you’ll have learned:
- What the Python new line character is
- How to print without newlines in Python
- How to use multiline strings to print across multiple lines
Let’s get started!
Python Newline Character
In Python, the character to create a new line is the \n character. Wherever this character is inserted into a string, a new line will be created when the string is printed out. In essence, this character indicates where a line ends – any further characters will be printed on a new line.
Let’s take a look at an example of how you can use the Python newline character,
\n, to print on multiple lines in Python:
# Printing on Multiple Lines in Python string = "Welcome to datagy!\nLearn Python and Data Science!" print(string) # Returns: # Welcome to datagy! # Learn Python and Data Science!
We can see that by including the
\n newline character, that when the string was printed the text was split across multiple lines.
As a fun aside, you may be thinking, “what if I actually want to print
'\n'?” Thankfully, this is very simple to do! The
\ character acts as the escape character. By prefixing this to the newline character, the newline character will be escaped. Let’s see what this looks like:
# Escaping the Newline Character in Python string = "Welcome to datagy!\\nLearn Python and Data Science!" print(string) # Returns: # Welcome to datagy!\nLearn Python and Data Science!
We can see that the escape character isn’t printed, but that the newline character is printed in full.
Printing in Python Without Newlines
When you print multiple statements in Python, by default they print across multiple lines. Take a look at the code below as an example:
# Printing Multiple Statements in Python print("Hello!") print("Welcome to datagy!") # Returns: # Hello! # Welcome to datagy!
The reason that these statements print across different lines is because the
print() function has a parameter
end= which defaults to the newline character,
We can actually change the ending of these print statements by passing in a different string. For example, if we wanted to keep printing multiple statements on the same output line, we could simply write:
# Printing in Python Without Newlines print("Hello!", end=' ') print("Welcome to datagy!") # Returns: # Hello! Welcome to datagy!
In the example above, we modified the
end= parameter to simply be a space character. Because of this, the two statements were printed on the same line, separated only by a single space.
Newlines in Python Multiline Strings
Another way in which you can easily print across multiple lines in Python is to use multi-line strings. Multi-line strings in Python are declared by triple single quotes or triple double quotes. This allows you to use line breaks within the string without explicitly declaring them.
Let’s take a look at how we can replicate our earlier example using multi-line strings:
# Printing on Multiple Lines in Python string = """Hello! Welcome to datagy!""" print(string) # Returns: # Hello! # Welcome to datagy!
Because we declared the string as a multi-line string, we were able to handle the linebreaks implicitly!
How to Remove New Lines in Python
We can use various string methods to remove leading and trailing newline characters. The
.lstrip() methods to remove leading and trailing whitespace (including newline characters) respectively. Let’s see how we can use the
.lstrip() method to remove a leading newline character:
# Removing a Trailing Newline Character in Python string = "\nHello! Welcome to datagy!" print(string) print(string.lstrip()) # Returns: # # Hello! Welcome to datagy! # Hello! Welcome to datagy!
In the example, we first printed the string with the leading newline character. Then, we printed the line by left stripping out any whitespace.
In this tutorial, you learned how to work with the newline character in Python. You learned how the new line character works, how to escape the new line character and how to print without the default new line argument. Then, you learned how to use multi-line strings to emulate printing on new lines. Finally, you learned how to remove new lines from strings in Python.
To learn about related topics, check out the tutorials below: