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

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))
主站蜘蛛池模板: 武平县| 广西| 得荣县| 黑山县| 南丰县| 上杭县| 浑源县| 会理县| 大埔县| 麟游县| 荔浦县| 竹山县| 时尚| 若尔盖县| 万载县| 泰来县| 泊头市| 遂宁市| 辽阳县| 福州市| 盐边县| 洪泽县| 潜江市| 黎城县| 安陆市| 通道| 宝清县| 城口县| 涟水县| 揭阳市| 盐池县| 海宁市| 同仁县| 海城市| 漳平市| 珠海市| 吉隆县| 长海县| 唐山市| 黄平县| 凤翔县|