0
0
Pythonprogramming~5 mins

Writing multiple lines in Python

Choose your learning style9 modes available
Introduction

Sometimes you want to write more than one line of text at once. This helps you keep your code neat and easy to read.

When you want to print a paragraph or multiple sentences.
When you need to store a long message in a variable.
When writing text that includes line breaks, like a poem or address.
When you want to create a multi-line string for documentation or notes.
Syntax
Python
'''This is a
multi-line string'''

"""This is also a
multi-line string"""
Use triple single quotes ''' or triple double quotes """ to write multiple lines.
The text inside keeps the line breaks exactly as you write them.
Examples
This example stores a multi-line message in a variable and prints it.
Python
message = '''Hello,
Welcome to Python!'''
print(message)
This prints three lines directly using triple double quotes.
Python
print("""Line one
Line two
Line three""")
Sample Program

This program stores a short poem in a multi-line string and prints it exactly as written.

Python
poem = '''Roses are red,
Violets are blue,
Python is fun,
And so are you.'''

print(poem)
OutputSuccess
Important Notes

Make sure to use the same type of triple quotes at the start and end.

Indentation inside the triple quotes is preserved, so be careful with spaces.

Summary

Use triple quotes to write multiple lines of text easily.

Multi-line strings keep line breaks and spaces as you type them.

This helps when printing or storing long text blocks.