How to print a line break in Python
Table of Contents
When you're working with strings, knowing how to create a line break is very useful.
A line break allows you to define when a section of text should be broken into multiple lines.
Rather than printing text multiple times, each on a different line, you can define a single text string that contains line breaks.
In this post, we'll look at how to use line breaks in Python.
Using line breaks in Python
The easiest way to use line breaks in Python is to use the \n
character. This character indicates that the text that follows after it will be on a new line.
Simply include the \n
character in your string when you want to break the text into multiple lines.
Here's an example of a 3-line string:
PYTHONstring = "This is the first line.\nThis is the second line.\nThis is the third line."
print(string)
BASHThis is the first line.
This is the second line.
This is the third line.
Another useful way to use line breaks is when you're in a loop.
PYTHONstring = ""
for i in range(3):
string += "This is line " + str(i) + ".\n"
print(string)
BASHThis is line 0.
This is line 1.
This is line 2.
Conclusion
In this post, we learned about how to use line breaks in Python.
To create a line break, simply include the \n
character in your string.
Thanks for reading and happy coding!
- Getting Started with TypeScript
- Getting Started with Solid
- Getting Started with Electron
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- How to deploy an Express app using Docker
- How to deploy a Node app using Docker
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Moment.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up a Local Web Server using Node.js