dfsfg sfg dfsfg sfg - 2 months ago 9
Python Question

How to make equal space between words - python

(SORRY FOR BAD ENGLISH)
Im working at cmd.
I want to do that thing:

file_name DIR
file_name_3 DIR
file_name_545 DIR
file_name_LlK DIR


Instead of doing that thing:

file_name DIR
file_name_3 DIR
file_name_545 DIR
file_name_LlK DIR


I tryied to do this in loop:

print data.ljust((20 - len(data) + 20)) if len(data) <= 20 else (data[0:17] + '...').ljust(20)), 'DIR'


But that thing not working becase there are letters bigger then another, then 'ljust' the words makes it not possible.

Answer

Use format strings.

"{:20}{}".format(data,"DIR")

The same with ljkust()

data.ljust(20) + "DIR"

See help(str.ljust) to understand ljust

Comments