3.7.7 - String Formatting

来源:互联网 发布:网上开淘宝店流程 编辑:程序博客网 时间:2024/05/18 00:49
a = 1b = 2c = 4.32456#Use the format method to replace {0} with the sum of a + b"The sum of a + b is {0}".format(a + b)"a: {0}  b: {1} c: {2}".format(a, b, c)"a: " + str(a) + " b: " + str(b) + " c: " + str(c)"a:{0:04d}".format(a)"c: {0:0.2f}".format(c)"The sum of a + b is %s" % ( a + b)"c: %0.2f" % (c)

 


运行结果如下:

"The sum of a + b is {0}".format(a + b)

// Result: The sum of a + b is 3 //

"a: {0} b: {1} c: {2}".format(a, b, c)

// Result: a: 1 b: 2 c: 4.32456 //

"a: " + str(a) + " b: " + str(b) + " c: " + str(c)

// Result: a: 1 b: 2 c: 4.32456 //

"a:{0:04d}".format(a)

// Result: a:0001 //

"c: {0:0.2f}".format(c)

// Result: c: 4.32 //

"The sum of a + b is %s" % ( a + b)

// Result: The sum of a + b is 3 //

"c: %0.2f" % (c)

// Result: c: 4.32 //


%()  类似.format()


0 0
原创粉丝点击