String formatting in Python

后端 未结 12 2163
-上瘾入骨i
-上瘾入骨i 2020-11-22 08:14

I want to do something like String.Format(\"[{0}, {1}, {2}]\", 1, 2, 3) which returns:

[1, 2, 3]

How do I do this in Python?

12条回答
  •  醉话见心
    2020-11-22 08:42

    If you don't know how many items are in list, this aproach is the most universal

    >>> '[{0}]'.format(', '.join([str(i) for i in [1,2,3]]))
    
    '[1, 2, 3]'
    

    It is mouch simplier for list of strings

    >>> '[{0}]'.format(', '.join(['a','b','c']))
    '[a, b, c]'
    

提交回复
热议问题