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

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))
主站蜘蛛池模板: 策勒县| 洛南县| 昆山市| 商水县| 同德县| 宁武县| 喀喇沁旗| 鲜城| 广东省| 综艺| 安康市| 礼泉县| 新化县| 九江县| 翼城县| 原阳县| 桑植县| 洛川县| 洛扎县| 林芝县| 张家港市| 顺昌县| 辽宁省| 清涧县| 江阴市| 鹤庆县| 延寿县| 江门市| 无为县| 海淀区| 如皋市| 武隆县| 察隅县| 延安市| 南江县| 娄烦县| 济南市| 革吉县| 吉木萨尔县| 佛坪县| 理塘县|