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

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))
主站蜘蛛池模板: 保康县| 兴国县| 米脂县| 大连市| 云和县| 香河县| 开远市| 南漳县| 潮安县| 离岛区| 洛隆县| 阿坝| 怀来县| 拜泉县| 松溪县| 化州市| 五家渠市| 错那县| 祁连县| 买车| 紫金县| 永春县| 固镇县| 东莞市| 武宣县| 固阳县| 延庆县| 同仁县| 济宁市| 台北县| 水城县| 新乡县| 泸定县| 墨玉县| 昌江| 望江县| 鄂托克前旗| 天气| 阿坝县| 白银市| 墨玉县|