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

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))
主站蜘蛛池模板: 高平市| 桐庐县| 永川市| 巴彦县| 扬州市| 商河县| 广宁县| 郁南县| 龙江县| 哈巴河县| 贺州市| 东安县| 文安县| 驻马店市| 饶阳县| 新丰县| 祁门县| 仁布县| 安阳市| 维西| 玉环县| 高要市| 洱源县| 大丰市| 双柏县| 宿州市| 阿图什市| 确山县| 高邮市| 阳西县| 顺义区| 平潭县| 花垣县| 周口市| 宝山区| 仁寿县| 威远县| 泾源县| 叶城县| 德格县| 黑水县|