- 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))
推薦閱讀
- Unreal Engine:Game Development from A to Z
- 大數據導論:思維、技術與應用
- Word 2000、Excel 2000、PowerPoint 2000上機指導與練習
- 大學計算機基礎:基礎理論篇
- MCSA Windows Server 2016 Certification Guide:Exam 70-741
- Photoshop CS3特效處理融會貫通
- Associations and Correlations
- 計算機網絡技術基礎
- INSTANT Autodesk Revit 2013 Customization with .NET How-to
- 激光選區熔化3D打印技術
- 電腦日常使用與維護322問
- 水晶石影視動畫精粹:After Effects & Nuke 影視后期合成
- The DevOps 2.1 Toolkit:Docker Swarm
- FreeCAD [How-to]
- 菜鳥起飛五筆打字高手