-1

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
martineau
  • 119,623
  • 25
  • 170
  • 301
varun kumar
  • 133
  • 2
  • 8

1 Answers1

0

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))
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45