abi*_*kay 0 python printing string sum concatenation
我想打印数字和一些字符串,例如:
print("root: " + rootLeaf + " left:" + leftLeaf + "sum: " +(rootLeaf+leftLeaf) )
Run Code Online (Sandbox Code Playgroud)
这里“root”,“left”和“sum”是字符串,其中rootLeaf和leftleaf是整数,我想找到它们的总和。
我在这里检查了帖子,但无法实现整数之和(字符串连接中的数学运算)
rootLeaf = 4
leftLeaf = 8
print("root: " + str(rootLeaf)+ " left: " + str(leftLeaf) + " sum: {}".format(rootLeaf + leftLeaf))
Run Code Online (Sandbox Code Playgroud)
这是你的输出:
root: 4 left: 8 sum: 12
Run Code Online (Sandbox Code Playgroud)
在Python中,为了打印整数,必须首先将它们转换为字符串。.format 方法允许您将整数参数(两个叶子的总和)转换为字符串。您在想要字符串 {} 的位置输入占位符,然后在 .format 方法中指定该整数是什么。在本例中,rootLeaf + leftLeaf。
| 归档时间: |
|
| 查看次数: |
7702 次 |
| 最近记录: |