String formatting in Python

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

    Before answering this question please go through couple of articles given below:

    Python Official Docs here

    Useful article:

    • Python String format() - here

    Now let's answer this question

    Question: 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?

    Answer:

    Well this is certainly a one-line code answer which is

    print("[{0},{1},{2}]".format(1, 2, 3))

    When you execute this one-line code a list containing three values as [1, 2, 3] will be printed. I hope this was pretty simple and self-explanatory.

    Thanks

    Tanu

提交回复
热议问题