How to replace custom tabs with spaces in a string, depend on the size of the tab?

前端 未结 12 2095
無奈伤痛
無奈伤痛 2021-01-17 15:13

I\'m trying to write a python function not using any modules that will take a string that has tabs and replace the tabs with spaces appropriate for an inputted tabstop size.

12条回答
  •  失恋的感觉
    2021-01-17 15:43

    This code can help you:

    initial_string = "My \tstring \ttest\t"
    block_size = "5"
    "".join([("{block_value:"+str(block_size)+"s}").format(block_value=block) 
        for block in initial_string.split("\t")])
    

    You will need to study: format, split and join function and list comprehension concept.

提交回复
热议问题