expandtabs is a method specified in Strings in Python 3.
Sometimes, there is a need of specifying the space in the string, but amount of space to be left is uncertain and depending upon the environment and conditions. For these cases, need to modify the string again and again is a tedious task. Hence python in its library has “expandtabs()” which specifies the amount of space to be substituted with the “\t” symbol in the string.
Syntax : expandtabs(space_size)
space_size : Specifies the space that is to be replaced with the “\t” symbol in the string. By default the space is 8.
Returns : Returns the modified string with tabs replaced by the space.
Code #1 : Code to demonstrate expandtabs()
Modified string using default spacing: i love gfg Modified string using less spacing: i love gfg Modified string using more spacing: i love gfg
The exception using this method is that it doesn’t accept the floating point number if we want to decide the exact precision of the space we require.
Code #2 : Code to demonstrate exception of expandtabs()
Modified string using default spacing: Error !! The error occurred is : integer argument expected, got float
There are many possible applications where this can be used such as text formatting or documentations where user requirements keep on changing.
- Python | Numpy expandtabs() method
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- Python Strings encode() method
- Python Strings decode() method
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python | Interleaving two strings
- C strings conversion to Python
- Python - Alternate Strings Concatenation
- Python | Extract K sized strings
- How To Index and Slice Strings in Python?
- Python | Identical Strings Grouping
- Python | Union Operation in two Strings
- Python | Scramble strings in list
- Python | Removing strings from tuple
- Python - Groups Strings on Kth character
- Python - Combine Strings to Matrix
- Python | Strings length summation
- Python - Characters which Occur in More than K Strings
- Python - Selectively Split in Strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.