1

I want to output text like so:

Якета            : **************************** 1250.23 €
Обувки за футбол : ********************** 912.30 €
Екипи            : ************** 513.45 €
Топки            : ************ 502.52 €
T-SHIRTS         : ********* 420.19 €

How can I use placeholders to indent all colons to the length of the longest string - in this case Обувки за футбол?

Idos
  • 15,053
  • 14
  • 60
  • 75
Alex
  • 715
  • 1
  • 8
  • 29
  • 1
    Get the `len` of the string, subtract it from the `len` of the longest string, and add that many spaces to the end of the string. – Mark Ransom Feb 02 '16 at 12:46

2 Answers2

2

Probably, the most elegant way is to use the format method. It allows to easily define the space a string will use:

>>> name = 'Якета'
>>> asterisks = '****************************'
>>> price = 1250.23
>>> print '{0:17}: {1} {2} €'.format(name, asterisks, price)
Якета       : **************************** 1250.23 €

Should you need to programmatically define padding size (for instance, to dynamically accept larger strings instead of hard-coding its size), simply use ljust:

>>> name = 'Якета'
>>> asterisks = '****************************'
>>> price = 1250.23
>>> padding = 17
>>> print '{0}: {1} {2} €'.format(name.ljust(padding), asterisks, price)
Якета       : **************************** 1250.23 €

Considering the case when the maximum string size is unknown previously and the script must adapt to it, we only need to calculate the maximum string size and place it in padding:

>>> names = ['abc', 'defghijklm', 'op', 'q']
>>> asterisks = '****************************'
>>> price = 1250.23
>>> padding = max(map(len, strings))
>>> for name in names:
        print '{0}: {1} {2} €'.format(name.ljust(padding), asterisks, price)
abc       : **************************** 1250.23 €
defghijklm: **************************** 1250.23 €
op        : **************************** 1250.23 €
q         : **************************** 1250.23 €

This thread has a pretty similar issue.

Community
  • 1
  • 1
Matheus Portela
  • 2,420
  • 1
  • 21
  • 32
  • 2
    Thanks, but this much I figured out. My problem is involving this method in dynamic environment. Let's say we don't know the length of the longest string to be printed, thus we can't hardcode the indents inside the placeholder: {0:17}. Is there a nested placeholder option I can use for this case? – Alex Feb 02 '16 at 13:05
  • That's what my second example does. Just put your longest string size in the `padding` variable and everything will be alright and Pythonic. Say you have a `strings = ['abc', 'defghijklm', 'op', 'q']` list with all yours strings. Then, the padding can be calculated using `padding = max(map(len, strings))`, which makes `padding = 10`. – Matheus Portela Feb 02 '16 at 14:09
  • I've just updated the answer to include this case, Alex – Matheus Portela Feb 02 '16 at 14:15
  • 1
    Also, you could consider dynamically creating your placeholders: `myplaceholder = '{0:' + str(max(map(len, strings))) + '}'` and then call `myplaceholder.format(...)`. I know I've made scripts using this type of formatting to output tables in ASCII. – DainDwarf Feb 02 '16 at 14:21
1

I would do something like this (somewhat "hackish"):

  • Get the longest string longest = len(longest_string). If you have your strings in a list then longest = len(max(mylist, key=len)).
  • Calculate for all strings spaces = longest - len(str).
  • Add spaces spaces to every end of string.
Idos
  • 15,053
  • 14
  • 60
  • 75
  • I thought of that, but I figured python has to have something cleaner wot work with. – Alex Feb 02 '16 at 13:08
  • @Alex I see, well this is the first thing that came to mind - easy to come up with, understand and implement :) – Idos Feb 02 '16 at 13:09
  • 1
    Well thanks. I shall implement it, since I can't think of or find anything I like better so far. – Alex Feb 02 '16 at 13:16