String formatting in Python

后端 未结 12 2001
-上瘾入骨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:50

    You're looking for string formatting, which in python is based on the sprintf function in C.

    print "[%s, %s, %s]" % (1, 2, 3)
    

    For a complete reference look here: http://docs.python.org/library/stdtypes.html#string-formatting

提交回复
热议问题