How to Indent Multiple Lines in Python?

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:

  1. Select multiple lines – Click and drag to highlight the lines you want to indent
  2. Press Tab – To indent the selected lines to the right
  3. 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 line

You can see the output in the below screenshot.

indentation in python

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: 97

You can see the output in the below screenshot.

what is indentation in python

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:

  1. Block selection (Alt+mouse drag in many editors)
  2. Multiple cursor editing (typically Ctrl+click or Alt+click)
  3. 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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.