- 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))
推薦閱讀
- Verilog HDL數(shù)字系統(tǒng)設(shè)計入門與應(yīng)用實例
- Hands-On Data Science with SQL Server 2017
- Mobile DevOps
- 樂高創(chuàng)意機器人教程(中級 下冊 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實踐指導(dǎo)叢書)
- 21天學(xué)通ASP.NET
- 21天學(xué)通Java
- 現(xiàn)代傳感技術(shù)
- 計算機網(wǎng)絡(luò)安全
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·工業(yè)機器人
- Machine Learning with the Elastic Stack
- 激光選區(qū)熔化3D打印技術(shù)
- Linux內(nèi)核精析
- 人工智能:語言智能處理
- PLC與變頻技術(shù)應(yīng)用
- 單片機技術(shù)項目化原理與實訓(xùn)