I have program which print the following
Test1 234
This is okay 1234
Bet 23
How to format into the following using python?
Test1 234
This is okay 1234
Bet 23
I have program which print the following
Test1 234
This is okay 1234
Bet 23
How to format into the following using python?
Test1 234
This is okay 1234
Bet 23
You can use the ljust
("left justify") method that strings have. For example, 'hi'.ljust(5)
would be 'hi '
.
Suppose your data was arranged like
data = [('Test1', 234), ('This is okay', 1234), ('Bet', 23)]
You would first determine the maximum length:
max_len = max(len(x[0]) for x in data)
Then
for (msg, num) in data:
print('{} {}'.format(msg.ljust(max_len), num))