Python Expand Tabs Length Calculation

前端 未结 2 1046
别跟我提以往
别跟我提以往 2020-12-06 07:45

I\'m confused by how the length of a string is calculated when expandtabs is used. I thought expandtabs replaces tabs with the appropriate number of spaces (with the default

相关标签:
2条回答
  • 2020-12-06 08:39

    The tab increments the column pointer to the next multiple of 8:

    >>> 'abc\tabc'.expandtabs().replace(' ', '*')
    'abc*****abc'
    
    0 讨论(0)
  • 2020-12-06 08:47

    Like when you are entering tabs in a text-editor, the tab character increases the length to the next multiple of 8.

    So:

    • '\t' by itself is 8, obviously.
    • '\t\t' is 16.
    • 'abc\tabc' starts at 3 characters, then a tab pushes it up to 8, and then the last 'abc' pushes it from 8 to 11...
    • 'abc\tabc\tabc' likewise starts at 3, tab bumps it to 8, another 'abc' goes to 11, then another tab pushes it to 16, and the final 'abc' brings the length to 19.
    0 讨论(0)
提交回复
热议问题