Python expandtabs string operation

前端 未结 2 709
北荒
北荒 2020-12-19 06:09

I am learning about Python and got to the expandtabs command in Python. This is the official definition in the docs:

string.expand         


        
2条回答
  •  一生所求
    2020-12-19 06:30

    The expandtabs method replaces the \t with whitespace characters until the next multiple of tabsize parameter i.e., the next tab position.

    for eg. take str.expandtabs(5)

    'this (5)is(7)\tstring' so the '\t' is replaced with whitespace until index=10 and follwing string is moved forward. so you see 10-7=3 whitespaces. (**number in brackets are index numbers **)

    eg2. str.expandtabs(4)

    'this(4) is(7)\tstring' here '\t' replaces until index=8. so you see only one whitespace

提交回复
热议问题