Python Reference Library →

Python String expandtabs() – Replace Tabs with Spaces

Let’s look at string expandtabs() – replace tabs with spaces

  1. Notice

    expandtabs() replaces tabs with spaces up to the next tab stop. The number of inserted spaces depends on the current column.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'a\tb'
result = text.expandtabs(4)
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'a\tb'Object 2 · str'a b'
This step

expandtabs() replaces tabs with spaces up to the next tab stop. The number of inserted spaces depends on the current column.

Picture string expandtabs() – replace tabs with spaces

expandtabs() replaces tabs with spaces up to the next tab stop. The number of inserted spaces depends on the current column.

Image brief

Alt text
A diagram for Python String expandtabs() – Replace Tabs with Spaces. Instructions: text = 'a\tb' result = text.expandtabs(4) print(result). Displayed result: a b. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'a\tb'; object 2: 'a b'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String expandtabs() – Replace Tabs with Spaces". Teaching focus: expandtabs() replaces tabs with spaces up to the next tab stop. The number of inserted spaces depends on the current column. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'a\tb' result = text.expandtabs(4) print(result) | v DISPLAYED RESULT a b FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: 'a\tb' object 2: 'a b' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String expandtabs() – Replace Tabs with Spaces. Instructions: text = 'a\tb' result = text.expandtabs(4) print(result). Displayed result: a b. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'a\tb'; object 2: 'a b'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'a\tb'
result = text.expandtabs(4)
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'a\tb'Object 2 · str'a b'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
a   b

What do you expect this program to print?

What does this example display?

text = 'a\tb'
result = text.expandtabs(4)
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

a b

Follow the printed values in order. The output is: a b

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

expandtabs() replaces tabs with spaces up to the next tab stop. The number of inserted spaces depends on the current column.

expandtabs() replaces tabs with spaces up to the next tab stop. The number of inserted spaces depends on the current column.

More examples and reference

Python String expandtabs() Method

The expandtabs() method in Python is used with string objects. It replaces all tab characters (\t) in a string with spaces. You can also set the number of spaces a tab should expand to.

Syntax

string.expandtabs(tabsize=8)

Parameters:

  • tabsize – (Optional) The number of spaces to replace each \t with. Default is 8.

Returns:

  • A new string where all \t tab characters are replaced with spaces.

Example 1: Default Tab Size

text = "Python\tRocks!"
print(text.expandtabs())
Python  Rocks!

Example 2: Custom Tab Size (tabsize=4)

text = "A\tB\tC"
print(text.expandtabs(4))
A   B   C

Example 3: With Multiple Tabs and Alignment

data = "Name\tAge\tCity"
print(data.expandtabs(10))
Name      Age       City

Why Use expandtabs()?

  • To make tabbed text align nicely using spaces
  • Useful in formatting outputs, reports, or logs
  • Helps standardize formatting across systems (tabs can display differently in different editors)

Common Mistakes

  • Forgetting that expandtabs() doesn’t modify the original string (strings are immutable)
  • Using it without understanding how tab size works with alignment (it aligns to the next multiple of tab size)

Interview Tip

This method might come up in string formatting problems where you need to normalize or align data.

Summary

  • expandtabs() replaces \t with spaces
  • Default tab size is 8, but you can customize it
  • Returns a new string (does not change original)

Practice Challenge

Write a Python program that takes a tab-separated string from user input and prints the expanded version using 6 spaces per tab.

user_input = input("Enter a tab-separated string: ")
print(user_input.expandtabs(6))

What to remember

Connect what you noticed in the example with the idea below.

Remember

expandtabs() replaces tabs with spaces up to the next tab stop. The number of inserted spaces depends on the current column.

Watch out for this idea

expandtabs(4) always inserts exactly four spaces per tab.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →