expandtabs acts unexpectedly

Peter Brett peter at peter-b.co.uk
Wed Aug 19 04:16:35 EDT 2009


"digisatori at gmail.com" <digisatori at gmail.com> writes:

> 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

-- 
Peter Brett <peter at peter-b.co.uk>
Remote Sensing Research Group
Surrey Space Centre



More information about the Python-list mailing list