To indent multiple lines in Python, select the lines in your editor and press Tab (or Ctrl+] in VS Code and IDLE). Shift+Tab or Ctrl+[ moves them back out. For a multi-line string inside your code, textwrap.indent() does the same job:
import textwrap
print(textwrap.indent("line one\nline two", " "))
Every run below happened on Windows 11 with Python 3.12.5, and the editor screenshots are from the real IDLE that ships with it.
We’ll go through the shortcuts for each editor, fixing an IndentationError by indenting a block, and indenting or dedenting strings with the textwrap module.
Keyboard shortcuts to indent multiple lines in each editor
Python groups statements by indentation, so every popular editor lets you shift a whole block: select first, then press the key.
| Editor | Indent selected lines | Dedent selected lines |
|---|---|---|
| VS Code | Tab or Ctrl+] (Cmd+] on Mac) | Shift+Tab or Ctrl+[ (Cmd+[) |
| PyCharm | Tab | Shift+Tab |
| IDLE | Ctrl+] or Format > Indent Region | Ctrl+[ or Format > Dedent Region |
| Jupyter Notebook / Lab | Ctrl+] | Ctrl+[ |
In PyCharm, Ctrl+Alt+I re-indents the selected lines to match the code around them. If you’re picking an editor, I compared both in PyCharm vs VS Code for Python.
The select-then-shortcut habit also covers comments. That’s a separate job with its own keys, explained in how to comment out multiple lines in Python.
Fix an IndentationError by indenting a block in IDLE
Here’s the most common real case. I pasted two lines under a for statement and forgot to indent them, so Python refuses to run the file:
orders = [19.99, 5.25, 102.00]
total = 0
for price in orders:
total += price
print(f"Running total: ${total:,.2f}")
print(f"Final total: ${total:,.2f}")
Output:
File "C:\pyguides\running_total.py", line 4
total += price
^
IndentationError: expected an indented block after 'for' statement on line 3

To fix it in IDLE, follow these steps:
- Open the file with File > Open.
- Drag across lines 4 and 5 to select them.
- Press Ctrl+], or choose Format > Indent Region from the menu bar.
- Save with Ctrl+S and press F5 to run the module.


Now the file runs. The unindented last print() sits outside the loop; see Python for loops for how loop blocks work:
orders = [19.99, 5.25, 102.00]
total = 0
for price in orders:
total += price
print(f"Running total: ${total:,.2f}")
print(f"Final total: ${total:,.2f}")
Output:
Running total: $19.99
Running total: $25.24
Running total: $127.24
Final total: $127.24
What causes an IndentationError in Python?
I compiled the four usual mistakes so you can match your own error message:
snippets = {
"missing indent": "for x in [1, 2]:\nprint(x)",
"unexpected indent": "total = 0\n total += 5",
"uneven dedent": "if True:\n a = 1\n b = 2",
"tabs and spaces": "if True:\n\ta = 1\n b = 2",
}
for name, src in snippets.items():
try:
compile(src, "demo.py", "exec")
except (IndentationError, TabError) as err:
print(f"{name:18} {type(err).__name__}: {err.msg}")
Output:
missing indent IndentationError: expected an indented block after 'for' statement on line 1
unexpected indent IndentationError: unexpected indent
uneven dedent IndentationError: unindent does not match any outer indentation level
tabs and spaces TabError: inconsistent use of tabs and spaces in indentation
Python’s style guide asks for four spaces per level and no tabs. Most editors insert spaces when you press Tab, but code pasted from the web can carry real tab characters. The same block rules apply to if-else statements in Python.
In VS Code, the status bar shows Spaces: 4 or Tab Size; click it and choose Convert Indentation to Spaces to clean up a whole file.
Indent a multi-line string with textwrap.indent()
Sometimes the lines are data, not code, like a receipt or an email body. textwrap.indent(text, prefix) adds the prefix to the start of each line:
import textwrap
receipt = "Coffee $4.50\nBagel $3.25\n\nTotal $7.75"
print(textwrap.indent(receipt, " ")) # four spaces on every line
print("---")
print(textwrap.indent(receipt, "> ")) # any prefix works
print("---")
print(textwrap.indent(receipt, "+ ", lambda line: True)) # blank lines too
Output:
Coffee $4.50
Bagel $3.25
Total $7.75
---
> Coffee $4.50
> Bagel $3.25
> Total $7.75
---
+ Coffee $4.50
+ Bagel $3.25
+
+ Total $7.75

Any prefix works, so email-style quoting is easy. If your text isn’t split into lines yet, see creating a string with newlines in Python.
Remove extra indentation with textwrap.dedent()
Triple-quoted strings inside a function pick up the function’s indentation. textwrap.dedent() removes the whitespace that every line shares and keeps the relative indents:
import textwrap
def shipping_note(city, total):
note = f"""
Order shipped to {city}
Items: 3
Total: ${total:,.2f}
"""
return textwrap.dedent(note).strip()
print(shipping_note("Austin, TX", 58.4))
print(repr(textwrap.dedent(" a\n b")))
Output:
Order shipped to Austin, TX
Items: 3
Total: $58.40
'a\n b'
The Items line keeps its extra two spaces because only the common margin is removed. For breaking long lines instead, read splitting a long string into multiple lines in Python.
Once your blocks line up, these guides are good next steps:
- Run a Python script
- Break and continue in Python
- Comment out a block of code in Python
- Single and double quotes in Python
Frequently asked questions
How do I indent multiple lines at once in Python?
Select the lines and press Tab. In VS Code, IDLE and Jupyter, Ctrl+] works too.
How do I unindent a block of code?
Select it and press Shift+Tab in VS Code or PyCharm, or Ctrl+[ in VS Code, IDLE and Jupyter.
Why do I get “expected an indented block”?
A line ending in a colon, like for or if, must be followed by at least one indented line.
Should I use tabs or spaces for Python indentation?
Use four spaces. Mixing tabs and spaces in one block raises TabError.
How do I indent every line of a string?
Use textwrap.indent(text, " "). Blank lines are skipped unless you pass a predicate.
Where are textwrap.indent and dedent documented?
See the textwrap module in the Python docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn