- 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))
推薦閱讀
- Apache Hive Essentials
- iClone 4.31 3D Animation Beginner's Guide
- 大數據平臺異常檢測分析系統的若干關鍵技術研究
- 塊數據5.0:數據社會學的理論與方法
- 21天學通C語言
- 面向對象程序設計綜合實踐
- 網站前臺設計綜合實訓
- TensorFlow Reinforcement Learning Quick Start Guide
- Dreamweaver CS6精彩網頁制作與網站建設
- Linux系統管理員工具集
- Hands-On Dashboard Development with QlikView
- Mastering Ansible(Second Edition)
- MPC5554/5553微處理器揭秘
- C#求職寶典
- 筆記本電腦維修之電路分析基礎