I want to print sum of numbers and some string like:
print("root: " + rootLeaf + " left:" + leftLeaf + "sum: " +(rootLeaf+leftLeaf) )
here "root", "left" and "sum" is string where as rootLeaf and leftleaf are integers and i want to find their sum.
I checked the post here but i couldn't achieve the sum of the integers (math operation in string concatination)
rootLeaf = 4
leftLeaf = 8
print("root: " + str(rootLeaf)+ " left: " + str(leftLeaf) + " sum: {}".format(rootLeaf + leftLeaf))
Here is your output:
root: 4 left: 8 sum: 12
In Python, in order to print integers, they must first be converted to strings. The .format method allows you to convert the integer argument (the sum of the two leafs) to strings. You enter a place holder where you want the string {}, and then in your .format method you can specify what that integer will be. In this case, rootLeaf + leftLeaf.