Python string expandtabs














































Python string expandtabs



Python String : expandtabs()


This article demonstrates how to use of expandtabs() method in string.

The expandtabs() method returns a copy of string with all tab characters ' ' replaced with whitespace characters until the next multiple of tabsize parameter.


Syntax :

string.expandtabs(tabsize)


Parameter Values :

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


Returns :

The expandtabs() returns a string where all ' ' characters are replaced with whitespace characters until the next multiple of tabsize parameter.


Sample Code :

1.

expandtabs() With no Argument :

str = 'xyz 12345 abc'

# no argument is passed
# default tabsize is 8
result = str.expandtabs()

print(result)
Output :
xyz     12345   abc


How expandtabs() works in Python?

The expandtabs() method keeps track of the current cursor position.

The position of first ' ' character in the above program is 3. And, the tabsize is 8 (if argument is not passed).

The expandtabs() character replaces the ' ' with whitespace until the next tab stop. The position of ' ' is 3 and the first tab stop is 8. Hence, the number of spaces after 'xyz' is 5.

The next tab stops are the multiples of tabsize. The next tab stops are 16, 24, 32 and so on.

Now, the position of second ' ' character is 13. And, the next tab stop is 16. Hence, there are 3 spaces after '12345'.




2.

expandtabs() With Different Argument :

str = "xyz 12345 abc"
print('Original String:'str)

# tabsize is set to 2
print('Tabsize 2:'str.expandtabs(2))

# tabsize is set to 3
print('Tabsize 3:'str.expandtabs(3))

# tabsize is set to 4
print('Tabsize 4:'str.expandtabs(4))

# tabsize is set to 5
print('Tabsize 5:'str.expandtabs(5))

# tabsize is set to 6
print('Tabsize 6:'str.expandtabs(6))
Output :
Original String: xyz    12345   abc                                                                                                           
Tabsize 2: xyz 12345 abc                                                                                                                      
Tabsize 3: xyz   12345 abc                                                                                                                    
Tabsize 4: xyz 12345   abc                                                                                                                    
Tabsize 5: xyz  12345     abc                                                                                                                 
Tabsize 6: xyz   12345 abc 


Explanation :

  • The default tabsize is 8. The tab stops are 8, 16 and so on. Hence, there is 5 spaces after 'xyz' and 3 after '12345' when you print the original string.
  • When you set the tabsize to 2. The tab stops are 2, 4, 6, 8 and so on. For 'xyz', the tab stop is 4, and for '12345', the tab stop is 10. Hence, there is 1 space after 'xyz' and 1 space after '12345'.
  • When you set the tabsize to 3. The tab stops are 3, 6, 9 and so on. For 'xyz', the tab stop is 6, and for '12345', the tab stop is 12. Hence, there are 3 spaces after 'xyz' and 1 space after '12345'.
  • When you set the tabsize to 4. The tab stops are 4, 8, 12 and so on. For 'xyz', the tab stop is 4 and for '12345', the tab stop is 12. Hence, there is 1 space after 'xyz' and 3 spaces after '12345'.
  • When you set the tabsize to 5. The tab stops are 5, 10, 15 and so on. For 'xyz', the tab stop is 5 and for '12345', the tab stop is 15. Hence, there are 2 spaces after 'xyz' and 5 spaces after '12345'.
  • When you set the tabsize to 6. The tab stops are 6, 12, 18 and so on. For 'xyz', the tab stop is 6 and for '12345', the tab stop is 12. Hence, there are 3 spaces after 'xyz' and 1 space after '12345'.


Hence,we can see above function expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not provided.


**********END OF ARTICLE **********


More Articles of Vishal Lodhi:

Name Views Likes
Python string zfill 114 0
Python string swapcase 92 0
Python string title 96 0
Python string startswith 102 0
Python string replace 144 0
Python string translate 84 0
Python string rpartition 85 0
Python string partition 74 0
Python string splitlines 113 0
Python string rsplit 79 0
Python string split 75 0
Python string rindex 81 0
Python string rfind 106 0
Python string upper 80 0
Python string lower 81 0
Python string maketrans 90 0
Python string strip 81 0
Python string rstrip 91 0
Python string lstrip 79 0
Python string rjust 96 0
Python string ljust 95 0
Python string len 81 0
Python string join 75 0
Python string casefold 82 0
Python string isprintable 84 0
Python string encode 78 0
Python string isdecimal 84 0
Python string isidentifier 78 0
Python string isupper 77 0
Python string istitle 79 0
Python string isspace 100 0
Python string isnumeric 101 0
Python string isdigit 82 0
Python string islower 84 0
Python string isalpha 95 0
Python string isalnum 101 0
Python string index 87 0
Python string find 93 0
Python string expandtabs() 81 0
Python string endswith 91 0
Python string count 86 0
Python string capitalize 77 0
Python string center 84 0
Python string Introduction 98 0
Python string Template 139 0
Python string Formatter 144 0
Python string printable 199 0
Python string whitespace 146 0
Python string punctuation 103 0
Python string octdigits 86 0
Python string hexdigits 103 0
Python string digits 79 0
Python string ascii_letters 344 0
Python string ascii_lowercase 146 0
Python string ascii_uppercase 156 0
How to Create Download Manager in Python 523 0
Python random weibullvariate 164 0
python random paretovariate 133 0
Python random vonmisesvariate 178 0
Python random normalvariate 195 0
Python random lognormvariate 119 0
Python random gauss 120 0
Python random gammavariate 131 0
Python random expovariate 194 0
Python random betavariate 184 0
Python random triangular 116 0
Python random uniform 140 0
Python random random 116 0
Python random sample 182 0
Python random shuffle 178 0
Python random choices 234 0
Python random choice 165 0
Python random randint 148 0
Python random randrange 214 0
Python random getrandbits 140 0
Python random setstate 136 0
Python random getstate 178 0
Python random seed 168 1
Python random Introduction 175 0

Comments