Single format descriptor for list

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Jan 20 06:46:08 EST 2016


On 20/01/2016 09:35, Paul Appleby 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's a useful recipe here 
http://code.activestate.com/recipes/577845-format_iter-easy-formatting-of-arbitrary-iterables 
hence.

 >>> a=range(4,8)
 >>> from myutils import format_iter
 >>> print(format_iter(a, fmt='{}th', sep='\n'))
4th
5th
6th
7th

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list