Python bytes.expandtabs() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

bytes.expandtabs() returns a new bytes object where every tab byte (\t, value 9) is replaced by one or more spaces so that the text after it starts at the next tab stop. Tab stops are every tabsize columns, 8 by default.

The column counter starts at zero and goes back to zero after each \n or \r byte. Every other byte, including non-ASCII ones, counts as one column, which is only correct for single-byte text.

bytes.expandtabs() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.expandtabs(...). No import is needed, and bytearray objects have the same method.

Syntax

Optionally pass the distance between tab stops.

python
result = data.expandtabs()
result = data.expandtabs(tabsize)
CallWhat it does
b"a\tb".expandtabs()Returns b'a b': 7 spaces to column 8.
b"a\tb".expandtabs(4)Returns b'a b'.
b"a\tb".expandtabs(0)Returns b'ab': tabs are removed.

A tab right at a tab stop still produces a full tabsize run of spaces.

Method signatures
python
bytes.expandtabs(tabsize=8)
# returns a new bytes object (bytearray.expandtabs() returns a bytearray)

Parameters

ParameterDescription
tabsizeOptional. Number of columns between tab stops. Default 8. Zero or a negative value removes the tabs.

Return Value

Return ValueDescription
bytesA copy with tabs replaced by spaces.

Python Examples

Example 1 — See Exactly How Many Spaces Are Inserted

Replace spaces with dots after expanding so the padding becomes visible, first with the default stops and then with a tab size of 4.

python
row = b"a\tbc\tdef"

print(row.expandtabs())
print(row.expandtabs().replace(b" ", b"."))
print(row.expandtabs(4).replace(b" ", b"."))

How It Works

  1. To column 8. After a the column is 1, so the first tab adds 7 spaces.
  2. To column 16. After bc the column is 10, so the second tab adds 6 spaces.
  3. Smaller stops. With tabsize=4 the tabs pad to columns 4 and 8, adding 3 and then 2 spaces.

Example 2 — Align a Tab-Separated Report

Tab-separated bytes look ragged when printed. Expanding to stops 10 columns apart lines up the fields of every row.

python
lines = [b"ID\tNAME\tSIZE", b"7\tlog\t120", b"12\tdata.bin\t4096"]

for line in lines:
    print(line.expandtabs(10).decode("ascii"))

How It Works

  1. First column. Each ID is padded to column 10, whatever its length.
  2. Second column. Names end at different columns, but every one is padded to column 20.
  3. Print as text. decode() turns the ASCII bytes into a str so the rows print without the b'...' wrapper.

Example 3 — Newlines Reset the Column Counter

Expand tabs in two lines at once, then remove the tabs completely with a tab size of zero.

python
text = b"ab\tc\nabcde\tf"

print(text.expandtabs(4).split(b"\n"))
print(text.expandtabs(0))

How It Works

  1. First line. After ab (column 2) the tab pads 2 spaces to column 4.
  2. Fresh count. The \n resets the column to 0, so after abcde (column 5) the tab pads 3 spaces to column 8.
  3. Tab size zero. expandtabs(0) deletes each tab and keeps the newline.

Frequently Asked Questions

It is 8 columns.
No. It pads to the next tab stop, so the number of spaces depends on the current column.
A newline (\n) or carriage return (\r) byte.
Call expandtabs(0), or data.replace(b"\t", b"").
Not for alignment, because each byte counts as one column. Decode to str first for multi-byte characters.

Next: bytes.find()

Return the lowest index of a byte sequence, or -1 if it is not found.

find() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful