Tabs in print are not consistent python

后端 未结 5 1150
庸人自扰
庸人自扰 2021-01-11 12:54

I want to have real tabs in a print but \\t only puts spaces. Eg:

 first:ThisIsLong         second:Short
 first:Short         second:ThisIsLong
         


        
5条回答
  •  失恋的感觉
    2021-01-11 13:44

    Instead of using tab (\t), I suggest to use string formatting using printf-style formatting or str.format:

    rows = [
        ['first:ThisIsLong', 'second:Short'],
        ['first:Short', 'second:ThisIsLong'],
        ['first:ThisIsEvenLonger', 'second:Short'],
    ]
    for first, second in rows:
        print('%-25s %s' % (first, second))
    

    or

    print('{:<25} {}'.format(first, second))
    

提交回复
热议问题