How to Use String expandtabs() Method in PythonPython Basics


The expandtabs() method converts the tab symbol(‘t’) in a string into a whitespace.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-


str = "JasontStanson istthe most famous actor in Hollywood.!!!"

print("The original string is: " + str)
print("str.expandtabs() is: " + str.expandtabs())
print("str.expandtabs(16) is: " + str.expandtabs(16))

Output:

The original string is: Jason	Stanson is	the most famous actor in Hollywood.!!!
str.expandtabs() is: Jason   Stanson is      the most famous actor in Hollywood.!!!
str.expandtabs(16) is: Jason           Stanson is      the most famous actor in Hollywood.!!!

Syntax

str.expandtabs(tabsize=8)

Parameters

The expandtabs() takes an integer tabsize argument. The default value of tabsize is 8.

Return Value

It returns a new string where all ‘t’ characters are replaced with whitespace characters until the next multiple of the tabsize parameter.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us