String formatting in Python

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

    Very short answer.

    example: print("{:05.2f}".format(2.5163)) returns 02.51

    • {} Set here Variable
    • : Start Styling
    • 0 leading with zeroes, " " leading with whitespaces
    • 5 LENGTH OF FULL STRING (Point counts, 00.00 is len 5 not 4)
    • .2 two digit after point, with rounding.
    • f for floats

提交回复
热议问题