English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutorial Básico Python

Controle de Fluxo Python

Função do Python

Tipos de Dados do Python

Operações de Arquivos Python

Objetos e Classes Python

Data e Hora Python

Conhecimento Avançado Python

Manual de Referência Python

Uso e exemplo do método expandtabs() da string Python

Python string methods

O método expandtabs() retorna uma cópia da string, onde todos os tabuladores '\t' são substituídos por caracteres de espaço até o próximo múltiplo do parâmetro tabsize.

A sintaxe do método expandtabs() é:

string.expandtabs(tabsize)

parâmetro expandtabs()

expandtabs() usa o parâmetro inteiro tabsize. O valor pré-definido de tabsize é8.

expandtabs() returns value

expandtabs() returns a string where all'\ t'All characters are replaced with space characters until the next multiple of the tabsize parameter.

Example1:Without parameters expandtabs()

str = 'xyz\t12345\tabc'
# No parameters passed
# Default tabsize is8
result = str.expandtabs()
print(result)

When running the program, the output is:

xyz     12345   abc

How does expandtabs() work in Python?

expandtabs() method tracks the current cursor position.

In the first'\ t'the position of the character is3. And, the position of tabsize is8(if no parameter is passed).

The expandtabs() character is replaced with spaces'\ t', until the next tab stops." \ t"the position of3, the first tab is8. Therefore, the number of spaces after "xyz" is5.

The next tab is at the multiple of tabsize.16,24,32, and so on.

Now, the second'\ t'the position of the character is13. And, the next tab is16. Therefore, in '12345'after3a space.

Example2:With different parameters of expandtabs()

str = "xyz\t12345\tabc"
print('Original string:', str)
# tabsize is set to2
 2:', str.expandtabs(2))
# tabsize is set to3
 3:', str.expandtabs(3))
# tabsize is set to4
 4:', str.expandtabs(4))
# tabsize is set to5
 5:', str.expandtabs(5))
# tabsize is set to6
 6:', str.expandtabs(6))

When running the program, the output is:

Original string: xyz	12345	abc
Tabsize 2: xyz 12345 abc
Tabsize 3: xyz   12345 abc
Tabsize 4: xyz 12345   abc
Tabsize 5: xyz  12345     abc
Tabsize 6: xyz   12345 abc

Usage explanation

  • The default tabsize value is8. The tab is8,16, and so on. Therefore, when you print the original string, after "xyz"5spaces, " 12345" after3a space.

  • Set tabsize to2when. The tab is2,4,6,8, and so on. For the "xyz", the tab is4, for the " 12345", the tab is10. Therefore, after "xyz"1a space, in the " 12345After the quote1a space.

  • Set tabsize to3when. The tab is3,6,9, and so on. For the "xyz", the tab is6, for the " 12345", the tab is12. Therefore, after "xyz"3a space, in the " 12345After the quote1a space.

  • Set tabsize to4when. The tab is4,8,12, and so on. For the "xyz", the tab is4, for the " 12345", the tab is12. Therefore, after "xyz"1a space, in the " 12345After the quote3a space.

  • Set tabsize to5when. The tab is5,10,15, and so on. For the "xyz", the tab is5, for the " 12345", the tab is15. Therefore, after "xyz"2a space, in the " 12345After the quote5a space.

  • Set tabsize to6when. The tab is6,12,18, and so on. For the "xyz", the tab is6, for the " 12345", the tab is12. Therefore, after "xyz"3a space, in the " 12345After the quote1a space.

Python string methods