0

So im trying to to add two int variables and printing it with a str but it doesnt seem to work

x = 10
y = 20
apple = "Number of apples "

sentence = apple +(x+y)
print(sentence)

2 Answers2

4

The code failed because you are trying to concatenate a string with an integer. Try to replace the 4th statement with the following:

sentence = apple + str(x+y)

The "str" function will convert the x+y expression result into a string, then the concatenation will be possible.

Husam
  • 43
  • 5
0

You can do sentence = apple + str(x+y), print(f'{apple} {x+y}'), or print('{}{}'.format(apple, x+y)

Acccumulation
  • 3,491
  • 1
  • 8
  • 12