As a Python developer, I have found that the readability and maintainability of code are really important. One common challenge many programmers face is managing indentation across multiple lines of code. In this comprehensive guide, I will walk you through various techniques to efficiently indent multiple lines in Python.
Indent Multiple Lines in Python
I will explain five important methods that will help to indent multiple lines in Python.
Read Differences Between List and Dictionary in Python?
Method 1: Use Tab or Space Key in IDEs
The most simple approach for indenting multiple lines is using your IDE’s built-in functionality.
In Popular Python IDEs:
- Select multiple lines – Click and drag to highlight the lines you want to indent
- Press Tab – To indent the selected lines to the right
- Press Shift+Tab – To dedent (move left) the selected lines
If you’re using PyCharm, you can simply highlight all the code you want to indent and press the tab key. This is one of the fastest methods for manual indentation.
Alternative Selection Methods
In some IDEs and text editors, you can select multiple lines by holding the Alt key and clicking on the beginning of each line you want to indent, then pressing the spacebar to add spaces to all selected lines simultaneously.
Check out Python Naming Conventions for Variables
Method 2: Use textwrap.indent() for String Indentation
If you need to use a program to indent multi-line strings (not code), Python’s textwrap module provides a useful solution.
import textwrap
original_text = """First line
Second line
Third line"""
indented_text = textwrap.indent(original_text, ' ') # 4 spaces
print(indented_text)Output:
First line
Second line
Third lineYou can see the output in the below screenshot.

The textwrap.indent() function efficiently pads each line with your specified indentation characters, making it perfect for formatting text output or documentation.
Read How to Remove Numbers from Strings in Python?
Method 3: Handle Multi-line Statements with Implicit Line Continuation
Python offers several ways to break long lines of code while maintaining proper indentation.
Use Parentheses, Brackets, or Braces
# Define the function before calling it
def calculate_total(subtotal, tax_rate, discount, shipping_cost):
return subtotal + tax_rate - discount + shipping_cost
# Long list broken across multiple lines
my_long_list = [
"item1",
"item2",
"item3",
"item4"
]
# Define variables for function call
subtotal = 100
tax_rate = 5
discount = 10
shipping_cost = 2
# Function call with multiple arguments
total = calculate_total(
subtotal,
tax_rate,
discount,
shipping_cost
)
print(f"Total: {total}")Output:
Total: 97You can see the output in the below screenshot.

When using parentheses, brackets, or braces, Python automatically handles the line continuation, and you can indent the continued lines for better readability.
Use Backslash for Line Continuation
long_string = "This is a very long string that " \
"continues on the next line and " \
"maintains proper indentation."Python allows us to break multi-line statements using backslashes, which can be particularly useful for long-string concatenations.
Check out Difference Between Functions and Methods in Python
Method 4: Use Triple Quotes for Multi-line Strings
For multi-line strings, triple quotes (”’ or “””) are ideal:
multi_line_string = """
This is a multi-line string
with proper indentation
that preserves whitespace.
"""However, be aware that this preserves all whitespace, including the indentation of each line. To maintain proper indentation for Python multiline strings, you may need to use the techniques mentioned above or carefully format your triple-quoted strings.
Read How to Square a Number in Python?
Method 5: Batch Indentation for Refactoring
When restructuring code, you might need to indent multiple lines at once as part of refactoring. Most Python IDEs support:
- Block selection (Alt+mouse drag in many editors)
- Multiple cursor editing (typically Ctrl+click or Alt+click)
- Find and replace with regular expressions
Conclusion
In this tutorial, I explained how to indent multiple lines in Python. I discussed five important methods to accomplish this task, such as using a tab or space key, using textwrap.indent() for string indentation, handling multi-line statements with implicit line continuation, using triple quotes, and batch indentation for refactoring.
You may like to read:
- How to Round Numbers to 2 Decimal Places in Python?
- How to Convert Decimal Numbers to Binary in Python?
- How to Generate Random Numbers Between 0 and 1 in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.