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

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))
主站蜘蛛池模板: 泗水县| 三都| 璧山县| 泸西县| 青海省| 武乡县| 涟源市| 精河县| 刚察县| 兴国县| 蚌埠市| 拉萨市| 龙口市| 北辰区| 巴中市| 正镶白旗| 赫章县| 石楼县| 凉山| 亳州市| 美姑县| 商城县| 大英县| 府谷县| 咸阳市| 莎车县| 麟游县| 邵阳县| 无为县| 海兴县| 铜梁县| 汽车| 始兴县| 鄂尔多斯市| 伽师县| 清流县| 阳新县| 鹤壁市| 榕江县| 白银市| 莱芜市|