官术网_书友最值得收藏!

The formatted string output

Let's revisit the example discussed in the previous section. We printed the result as follows:

    print("The sum is %d" % total)

In Python, it is possible to format a string to display the result. In the earlier example, we make use of %d to indicate that it is a placeholder for an integer variable. This enables printing the string with the integer. Along with the string that is passed an argument to the print() function, the variable that needs to be printed is also passed an argument. In the earlier example, the variables are passed using the % operator. It is also possible to pass multiple variables:

    print("The sum of %d and %d is %d" % (var1, var2, total))

It is also possible to format a string as follows:

    print("The sum of 3 and 2 is {total}".format(total=5))
主站蜘蛛池模板: 霍林郭勒市| 夹江县| 舒城县| 云龙县| 台湾省| 花垣县| 苏州市| 海阳市| 皋兰县| 克拉玛依市| 南江县| 建始县| 繁昌县| 莫力| 尉氏县| 汽车| 梨树县| 昔阳县| 鸡西市| 即墨市| 达孜县| 额济纳旗| 庆阳市| 苍溪县| 庆元县| 东乌珠穆沁旗| 阜新| 婺源县| 宁津县| 宁都县| 贵港市| 贵德县| 铁岭市| 黑河市| 通化市| 德惠市| 衡阳县| 通渭县| 松原市| 万山特区| 眉山市|