It will return copy of string where all tab(\t) characters are replaced with spaces. If tab size not given it will assume tab size of 8 characters.

Syntax of str.expandtabs():

Below is syntax of str expandtabs in python

expandtabs(tab size=integer)

a=’weather\tis\tgood\ttoday

a.expandtabs(2)

Output: ‘weather is good today’

It will expand tab characters with 2 char spaces.as shown below

how to write str.expandtabs in python
1.Str expandtabs() method in Python

If tab size not given it will assume tab size of 8 spaces. As first Position of the tab character is 8 in given string .It fills it with 8-7=1 char spaces.2nd position of the tab char in given string is from first tab char is 2 and it has inserted 8-2=6 char spaces,3rd tab char is present at 5th position from 2nd tab char therefor It inserts 3 8-5=3 spaces of char in output.

 

string expand tab in python without mentioning tab size
2.Str.expandtabs-Without tabsize

 

By SC

One thought on “Str.expandtabs”

Leave a Reply

Your email address will not be published. Required fields are marked *