textwrap.dedent() drops tabs - bug or feature?

Steven Bethard steven.bethard at gmail.com
Fri Nov 18 05:56:38 CET 2005

So I've recently been making pretty frequent use of textwrap.dedent() to 
allow me to use triple-quoted strings at indented levels of code without 
getting the extra spaces prefixed to each line.  I discovered today that 
not only does textwrap.dedent() strip any leading spaces, but it also 
substitutes any internal tabs with spaces.  For example::

py> def test():
...     x = ('abcd	efgh\n'
...          'ijkl	mnop\n')
...     y = textwrap.dedent('''\
...         abcd	efgh
...         ijkl	mnop
...         ''')
...     return x, y
py> test()
('abcd\tefgh\nijkl\tmnop\n', 'abcd    efgh\nijkl    mnop\n')

Note that even though the tabs are internal, they are still removed by 
textwrap.dedent().  The documentation[1] says:

     Remove any whitespace that can be uniformly removed from the left 
of every line in text.

     This is typically used to make triple-quoted strings line up with 
the left edge of screen/whatever, while still presenting it in the 
source code in indented form.

So it looks to me like even if this is a "feature" it is undocumented. 
I'm planning on filing a bug report, but I wanted to check here first in 
case I'm just smoking something.


[1] http://docs.python.org/lib/module-textwrap.html

More information about the Python-list mailing list