On 20 January 2016 at 09:35, Paul Appleby <pap@nowhere.invalid> wrote: > In BASH, I can have a single format descriptor for a list: > > $ a='4 5 6 7' > $ printf "%sth\n" $a > 4th > 5th > 6th > 7th > > Is this not possible in Python? Using "join" rather than "format" still > doesn't quite do the job: > >>>> a = range(4, 8) >>>> print ('th\n'.join(map(str,a))) > 4th > 5th > 6th > 7 > > Is there an elegant way to print-format an arbitrary length list?
There are many ways. Here's a couple: >>> print(('%sth\n' * len(a)) % tuple(a)) 4th 5th 6th 7th >>> print(('{}th\n' * len(a)).format(*a)) 4th 5th 6th 7th If you particularly like using map then: >>> print(''.join(map('%sth\n'.__mod__, a))) 4th 5th 6th 7th >>> print(''.join(map('{}th\n'.format, a))) 4th 5th 6th 7th -- Oscar -- https://mail.python.org/mailman/listinfo/python-list