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

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))
主站蜘蛛池模板: 玉林市| 甘孜县| 广东省| 宜兰县| 咸丰县| 灵宝市| 竹北市| 渝中区| 称多县| 乐业县| 仪陇县| 全南县| 驻马店市| 龙山县| 独山县| 西昌市| 聂拉木县| 泰安市| 历史| 佳木斯市| 延吉市| 靖远县| 防城港市| 介休市| 三江| 德保县| 白玉县| 湖南省| 平陆县| 监利县| 方城县| 高邑县| 长顺县| 莆田市| 郸城县| 青冈县| 卓资县| 西畴县| 密云县| 慈利县| 武汉市|