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

前端 未结 3 1815
野性不改
野性不改 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:13

    Use format:

    str = 'The {} are {}, {}, {}, and {}'.format('numbers', *lstData)
    

    see the docs for more details about possible formatting (floats, decimal points, conversion, ..).

提交回复
热议问题