- Python Programming with Raspberry Pi
- Sai Yamanoor Srihari Yamanoor
- 141字
- 2021-07-02 23:48:44
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))
推薦閱讀
- Mastering Mesos
- Spark編程基礎(chǔ)(Scala版)
- R Machine Learning By Example
- 精通Windows Vista必讀
- Windows XP中文版應(yīng)用基礎(chǔ)
- WordPress Theme Development Beginner's Guide(Third Edition)
- Pig Design Patterns
- Java Web整合開發(fā)全程指南
- Kubernetes for Developers
- Troubleshooting OpenVPN
- 教育機(jī)器人的風(fēng)口:全球發(fā)展現(xiàn)狀及趨勢(shì)
- Mastering pfSense
- SQL Server數(shù)據(jù)庫應(yīng)用基礎(chǔ)(第2版)
- Learning Cassandra for Administrators
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)實(shí)訓(xùn)·職業(yè)模塊