expandtabs acts unexpectedly

D

digisatori

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.'test test'

1st example: expect returning 4 spaces between 'test', 3 spaces
returned
2nd example: expect returning 5 spaces between 'test', 4 spaces
returned

Is it a bug or something, please advice.
 
P

Peter Brett

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.'test test'

1st example: expect returning 4 spaces between 'test', 3 spaces
returned
2nd example: expect returning 5 spaces between 'test', 4 spaces
returned

Is it a bug or something, please advice.

Consider where the 4-space tabstops are relative to those strings:

test test
test test
^ ^ ^

So no, it's not a bug.

If you just want to replace the tab characters by spaces, use:
'test test'

HTH,

Peter
 
D

digisatori

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
' test\ttest'.expandtabs(4) ' test   test'
'test \ttest'.expandtabs(4)
'test    test'
1st example: expect returning 4 spaces between 'test', 3 spaces
returned
2nd example: expect returning 5 spaces between 'test', 4 spaces
returned
Is it a bug or something, please advice.

Consider where the 4-space tabstops are relative to those strings:

 test   test
test    test
^   ^   ^

So no, it's not a bug.

If you just want to replace the tab characters by spaces, use:

  >>> " test\ttest".replace("\t", "    ")
  ' test    test'
  >>> "test \ttest".replace("\t", "    ")
  'test     test'

HTH,

                               Peter

You corrected me for the understanding of tab stop. Great explanation.
Thank you so much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top