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 decode() method
- Python Strings encode() 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
- Interesting facts about strings in Python | Set 1
- Python | Split Sublist Strings
- Python | Strings length summation
- Python | Reversed Split Strings
- Python | C Strings of Doubtful Encoding | Set-2
- Python | How to sort a list of strings
- Python | Similarity metrics of 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.