Tabs in print are not consistent python

后端 未结 5 1145
庸人自扰
庸人自扰 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:19

    You can use formatting to align the strings instead. For example, you can tell python that the first column should be 20 characters long, and the second should be 10, left aligned.

    For example:

    string_one = 'first:ThisIsLong'
    string_two = 'second:Short'
    
    print( '{:<20s} {:<10s}'.format(string_one, string_two) )
    

    will print:

    first:ThisIsLong     second:Short
    

    The first formatting descriptor ({:<20s}) here is saying:

    '<' left align, 20 at least 20 characters, s because it's a string

提交回复
热议问题