In this tutorial, we’ll learn what Python string expandtabs() method is and how to properly use it with the help of an easy and detailed Python code examples.
Outline
- Introduction: Python String expandtabs() Method
- Syntax of expandtabs() Method
- Example 1: expandtabs() having no Argument
- Working of expandtabs() Method
- Example 2: expandtabs() with an Argument
- Example 3: expandtabs() Return Value
- Conclusion
Introduction: Python String expandtabs() Method
This method is used to return a copy of the specified string having all the tabs replaced with whitespaces until the next parameter of tabsize parameter.
Syntax of expandtabs() Method
string.expandtabs(tabSize)
- Python string expandtabs() method takes one integer argument. It is optional. The default value is 8.
- This method returns a new string in which all the tabs‘\t’ are replaced with whitespace characters until the next multiple of tabSize parameter.
Example 1: expandtabs() having no Argument
stringValue='This\tis\tPython\tProgramming' print(stringValue) print(stringValue.expandtabs())
Output
This is Python Programming // default tabs This is Python Programming // tabs replaced with whitespaces
The default tab size value of expandtabs() method is 8. Let’s now see how to customize its value but first let’s see how Python string expandtabs() method works.
Working of expandtabs() Method
- The current cursor position is tracked by expandtabs().
- The tabSize is 8 by default and the position of first tab‘\t’ in the above original string is 4.
- This method replaces the tab character with whitespace character until the next tab stop. As mentioned above, the position of first tab‘\t’ is 4 and tabSize is 8. It means the number of spaces after This(string) is 4. The next tab is at position 10 so it will have 6 spaces and so on. Give it a try for better understanding.
- The next tab stops are multiples of tabSize which will be 16,24,32 etc.
Example 2: expandtabs() with an Argument
stringValue='This\tis\tPython\tProgramming\tLanguage' print(stringValue.expandtabs(5))
Output
This is Python Programming Language
You can try it by passing different integer arguments to Python string expandtabs method.
Example 3: expandtabs() Return Value
originalstring='This\tis\tPython\tProgramming\tLanguage' returnedVal=originalstring.expandtabs(5) print(returnedVal) print(originalstring)
Output
This is Python Programming Language // newly returned string This is Python Programming Language // original string
As we can see that expandtabs() method returns a new string in which tabs are replaced with whitespaces.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical knowledge of how to properly use Python string expandtabs method. Click here to understand expandtabs() method in more detail. I’ll be looking forward to have your valuable feedback on this post. Thank you for reading it.
You may like to read:
How To Use Python String Count() Method – Easy Python Code Examples
How To Use Python String CaseFold() Method – Easy Python Code Examples
How To Use Python Dictionary PopItem() Method – Easy Python Guide