- 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))
推薦閱讀
- Instant Raspberry Pi Gaming
- Practical Ansible 2
- Visualforce Development Cookbook(Second Edition)
- Mastercam 2017數控加工自動編程經典實例(第4版)
- 圖解PLC控制系統梯形圖和語句表
- Effective DevOps with AWS
- Security Automation with Ansible 2
- 傳感器技術應用
- RPA(機器人流程自動化)快速入門:基于Blue Prism
- 控制系統計算機仿真
- Nginx高性能Web服務器詳解
- Python:Data Analytics and Visualization
- AI的25種可能
- 電氣控制與PLC原理及應用(歐姆龍機型)
- ASP.NET 2.0 Web開發入門指南