Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.expandtabs

bytes — padding

Replace tab characters with spaces up to the next tab stop.

Syntax

b.expandtabs(...)

It does not swap each tab for a fixed number of spaces: it pads out to the next multiple of tabsize, so how much is inserted depends on how many bytes already sit to the left of the tab. The column counter resets at every newline and carriage return, so a multi-line block is processed line by line. The default tabsize is 8, which is where surprisingly wide gaps in output usually come from.

Examples

print(b'a\tb'.expandtabs(4))   # → b'a   b'
print(b'a\tb'.expandtabs())   # → b'a       b'
print(b'ab\tc'.expandtabs(4))   # → b'ab  c'
print(b'abcd\te'.expandtabs(4))   # → b'abcd    e'
print(b'a\tb\ncd\te'.expandtabs(4))   # → b'a   b\ncd  e'
print(b'a\tb'.expandtabs(0))   # → b'ab'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy