expandtabs is a method specified in Strings in Python 3.
Sometimes, there is a need of specifying the space in the string, but the amount of space to be left is uncertain and depending upon the environment and conditions. For these cases, the 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 of 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 documentation where user requirements keep on changing.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course