Easy Tutorial
❮ Python Func Number Sin Python Hcf ❯

Python3 expandtabs() Method

Python3 Strings


Description

The expandtabs() method converts the tab symbols \t in the string to spaces. The default number of spaces for the tab symbol \t is 8, providing tab positions at 0, 8, 16, etc. If the number of characters from the current position to the start position or the previous tab position is not a multiple of 8, it is replaced with spaces.

Syntax

The syntax for the expandtabs() method is:

str.expandtabs(tabsize=8)

Parameters

Return Value

This method returns a new string where the tab symbols \t are replaced with spaces.

Example

The following example demonstrates the expandtabs() method:

#!/usr/bin/python3

str = "tutorialpro\t12345\tabc"  
print('Original string:', str)

# Default 8 spaces
# "tutorialpro" has 6 characters, the following \t fills 2 spaces
# "12345" has 5 characters, the following \t fills 3 spaces
print('Replace \t symbols:', str.expandtabs())

# 2 spaces
# "tutorialpro" has 6 characters, which is exactly 3 times 2, the following \t fills 2 spaces
# "12345" has 5 characters, which is not a multiple of 2, the following \t fills 1 space
print('Replace \t symbols with 2 spaces:', str.expandtabs(2))

# 3 spaces
print('Replace \t symbols with 3 spaces:', str.expandtabs(3))

# 4 spaces
print('Replace \t symbols with 4 spaces:', str.expandtabs(4))

# 5 spaces
print('Replace \t symbols with 5 spaces:', str.expandtabs(5))

# 6 spaces
print('Replace \t symbols with 6 spaces:', str.expandtabs(6))

The output of the above example is:

Original string: tutorialpro      12345   abc
Replace \t symbols: tutorialpro  12345   abc
Replace \t symbols with 2 spaces: tutorialpro  12345 abc
Replace \t symbols with 3 spaces: tutorialpro   12345 abc
Replace \t symbols with 4 spaces: tutorialpro  12345   abc
Replace \t symbols with 5 spaces: tutorialpro    12345     abc
Replace \t symbols with 6 spaces: tutorialpro      12345 abc

Python3 Strings

❮ Python Func Number Sin Python Hcf ❯