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

Tutoriel de base Python

Contrôle de flux Python

Fonctions en Python

Types de données en Python

Opérations de fichiers Python

Objets et classes Python

Dates et heures Python

Connaissances avancées Python

Manuel de référence Python

Utilisation et exemple de expandtabs() dans les chaînes de caractères Python

Python string methods

La méthode expandtabs() retourne une copie de la chaîne de caractères, où tous les tabulations '\t' sont remplacés par des espaces jusqu'au prochain multiple de tabsize.

La syntaxe de la méthode expandtabs() est :

string.expandtabs(tabsize)

paramètre de expandtabs()

expandtabs() utilise le paramètre tabsize en entier. La valeur prédéfinie de tabsize est8.

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 Python.

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

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

The next tab is a multiple of tabsize. The next tab is16,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", there are3a space.

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

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

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

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

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

Python string methods