In the previous article, we have discussed Python String endswith() Method with Examples
String expandtabs() Method in Python:
The expandtabs() method expands(sets) the tabsize to the number of whitespaces specified.
The expandtabs() method returns a string copy with all tab characters ‘\t’ replaced with whitespace characters until the next multiple of the tabsize parameter is reached.
Syntax:
string.expandtabs(tabsize)
Parameters
tabsize: This is Optional. It is a number that specifies the tabsize. The default tabsize is eight(8).
Return Value:
expandtabs() returns a string in which all ‘\t’ characters are replaced with whitespace until the next multiple of the tabsize parameter is reached.
Examples:
Example1:
Input:
Given String = "hello\t this is\t btechgeeks\t"
Output:
The string after setting to the default tabsize(8): hello this is btechgeeks
Example2:
Input:
Given String = "good\t morning\t btechgeeks\t" Given tabsize = 5
Output:
The string after setting to the given tabsize{ 5 } is : good morning btechgeeks
String expandtabs() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
1)Without Giving Tabsize
Approach:
- Give the string as static input and store it in a variable.
- Apply expandtabs() method to the given string that expands(sets) the tabsize to the number of whitespaces specified. The default tabsize is eight(8).
- Store it in another variable.
- Print the string after setting to the default tabsize(8).
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "hello\t this is\t btechgeeks\t" # Apply expandtabs() method to the given string that expands(sets) the tabsize # to the number of whitespaces specified. The default tabsize is eight(8). # Store it in another variable. rslt = gvn_str.expandtabs() # Print the string after setting to the default tabsize(8). print("The string after setting to the default tabsize(8):") print(rslt)
Output:
The string after setting to the default tabsize(8): hello this is btechgeeks
2)With Giving Tabsize
Approach:
- Give the string as static input and store it in a variable.
- Give the tabsize as static input and store it in another variable.
- Apply expandtabs() method to the given string by passing the given tabsize as an argument that expands(sets) the tabsize to the given number of whitespaces specified. The default tabsize is eight(8).
- Store it in another variable.
- Print the string after setting it to the given number of whitespaces specified(tabsize).
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "good\t morning\t btechgeeks\t" # Give the tabsize as static input and store it in another variable. gvntab_size = 5 # Apply expandtabs() method to the given string by passing the given tabsize # as an argument that expands(sets) the tabsize to the given number of # whitespaces specified. The default tabsize is eight(8). # Store it in another variable. rslt = gvn_str.expandtabs(gvntab_size) # Print the string after setting it to the given number of whitespaces # specified(tabsize). print("The string after setting to the given tabsize{", gvntab_size, "} is :") print(rslt)
Output:
The string after setting to the given tabsize{ 5 } is : good morning btechgeeks
Similarly, check it out for the other tabsize.
# Similarly, check it out for the other tabsize. gvn_str = "good\t morning\t btechgeeks\t" gvntab_size = 10 rslt = gvn_str.expandtabs(gvntab_size) print("The string after setting to the given tabsize{", gvntab_size, "} is :") print(rslt)
Output:
The string after setting to the given tabsize{ 10 } is : good morning btechgeeks
# Similarly, check it out for the other tabsize. gvn_str = "good\t morning\t btechgeeks\t" gvntab_size = 1 rslt = gvn_str.expandtabs(gvntab_size) print("The string after setting to the given tabsize{", gvntab_size, "} is :") print(rslt)
Output:
The string after setting to the given tabsize{ 1 } is : good morning btechgeeks
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.