Issue 35612: Text wrap over text containing tab character

Issue35612

Created on 2018-12-29 11:48 by Devika Sondhi, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg332712 - (view) Author: Devika Sondhi (Devika Sondhi) Date: 2018-12-29 11:48
textwrap.wrap does not seem to preserve tab character ('\t') in the text if it is not separated from other characters by a space.
Example:
>>> textwrap.wrap("Here is\tone line of text that is going to be wrapped after 20 columns.",20)                                                           
['Here is one line of', 'text that is going', 'to be wrapped after', '20 columns.']
The tab is missing from the above output.
However, for text with \t separated by space, the behavior is as expected (shown below).
>>> textwrap.wrap("Here is \t one line of text that is going to be wrapped after 20 columns.",20)                                                         
['Here is          one', 'line of text that is', 'going to be wrapped', 'after 20 columns.']
msg332713 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-12-29 12:09
I think you may be misunderstanding what you are seeing.

The documentation for textwrap.wrap says:

    By default, tabs in 'text' are expanded with string.expandtabs()

which converts tabs to one or more spaces, enough to align to some 
multiple of column 8:

py> "He is\tone".expandtabs()
'He is   one'
py> "Her is\tone".expandtabs()
'Her is  one'
py> "Here is\tone".expandtabs()
'Here is one'
py> "THere is\tone".expandtabs()
'THere is        one'

Can you confirm that this is the behaviour you are seeing? If so, I 
think it is correct and this bug report can be closed.
msg332715 - (view) Author: Devika Sondhi (Devika Sondhi) Date: 2018-12-29 12:33
I wasn't aware of this. Thanks for clarifying
History
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79793
2018-12-29 12:33:26Devika Sondhisetstatus: open -> closed

messages: + msg332715
stage: resolved

2018-12-29 12:09:34steven.dapranosetnosy: + steven.daprano
messages: + msg332713
2018-12-29 11:48:17Devika Sondhicreate