Python - short way to unpack list for string formatting operator?

前端 未结 3 1784
野性不改
野性不改 2021-01-12 01:27

Variations of the * or ** operators don\'t seem to work, unfortunately:

lstData = [1,2,3,4]
str = \'The %s are %d, %d, %d, and %d\' % (\'numbers\', *lstData)         


        
3条回答
  •  被撕碎了的回忆
    2021-01-12 02:05

    >>> data = range(5)
    >>> 'The {0} are {1}, {2}, {3}, {4} and {5}'.format('numbers', *data)
    'The numbers are 0, 1, 2, 3 and 4'
    

提交回复
热议问题