Open In App

expandtabs() method in Python

Last Updated : 18 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Python String expandtabs() Method specifies the amount of space to be substituted with the “\t” symbol in the string.

Python String expandtabs() Method Syntax

Syntax : expandtabs(space_size)

Parameters

  • tabsize : Specifies the space that is to be replaced with the “\t” symbol in the string. By default the space is 8.

Return : Returns the modified string with tabs replaced by the space.

Python String expandtabs() Method Example

Example 1: expandtabs() With no Argument

Python3




string = "\t\tCenter\t\t"
print(string.expandtabs())


Output:

                Center          

Example 2: expandtabs() with different argument

Here, we have demonstrated multiple usages of the Python String expandtabs() Method with different tabsize parameter values.

Python3




# initializing string
string = "i\tlove\tgfg"
 
# using expandtabs to insert spacing
print("Modified string using default spacing: ", end ="")
print(string.expandtabs())
print()
 
# using expandtabs to insert spacing
print("Modified string using less spacing: ", end ="")
print(string.expandtabs(2))
print()
 
# using expandtabs to insert spacing
print("Modified string using more spacing: ", end ="")
print(string.expandtabs(12))
print()


Output:

Modified string using default spacing: i       love    gfg

Modified string using less spacing: i love  gfg

Modified string using more spacing: i           love        gfg

Exception :  Using expandtabs() on float or int types, raises AttributeError

TypeError when using expandtabs()

If we pass float or any other non-integer argument in “tabsize” parameter of Python String expandtabs() Method. It raises a TypeError.

Python3




string = "\tcenter\t"
print(string.expandtabs(1.1))


Output:

Traceback (most recent call last):
  File "/home/358ee0f95cc3b39382a3849ec716fc37.py", line 2, in <module>
    print(string.expandtabs(1.1))
TypeError: integer argument expected, got float

Applications : There are many possible applications where this can be used, such as text formatting or documentation, where user requirements keep on changing.



Previous Article
Next Article

Similar Reads

Python | Numpy expandtabs() method
With the help of numpy.char.expandtabs() method, we can expand the tab using numpy method in the single statement. Syntax : numpy.char.expandtabs() Return : Return the string array having expanded tabs. Example #1 : In this example we can see that by using numpy.char.expandtabs() method, we are able to get the expanded tabs by using numpy. # import
1 min read
Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace &amp; expandtabs())
Some of the string methods are covered in the below sets.String Methods Part- 1 String Methods Part- 2More methods are discussed in this article1. strip():- This method is used to delete all the leading and trailing characters mentioned in its argument.2. lstrip():- This method is used to delete all the leading characters mentioned in its argument.
4 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Class method vs Static method in Python
In this article, we will cover the basic difference between the class method vs Static method in Python and when to use the class method and static method in python. What is Class Method in Python? The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated after your function is defined. The result of that
4 min read
Difference between Method Overloading and Method Overriding in Python
Method Overloading: Method Overloading is an example of Compile time polymorphism. In this, more than one method of the same class shares the same method name having different signatures. Method overloading is used to add more to the behavior of methods and there is no need of more than one class for method overloading.Note: Python does not support
3 min read
Pandas DataFrame iterrows() Method | Pandas Method
Pandas DataFrame iterrows() iterates over a Pandas DataFrame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in the form of a series. Example: Python Code import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 32, 3
2 min read
Pandas DataFrame interpolate() Method | Pandas Method
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Python Pandas interpolate() method is used to fill NaN values in the DataFrame or Series using various interpolation techniques to fill the m
3 min read
Pandas DataFrame duplicated() Method | Pandas Method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas duplicated() method identifies duplicated rows in a DataFrame. It returns a boolean series which is True only for unique rows. Ex
3 min read
Python Dictionary get() Method
Python Dictionary get() Method return the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument). Python Dictionary get() Method Syntax: Syntax : Dict.get(key, default=None) Parameters: key: The key name of the item you want to return the value fromValue: (Optional) Value to
2 min read
Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
Edge detection is one of the fundamental image-processing tasks used in various Computer Vision tasks to identify the boundary or sharp changes in the pixel intensity. It plays a crucial role in object detection, image segmentation and feature extraction from the image. In Real-time edge detection, the image frame coming from a live webcam or video
5 min read