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)
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)
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.
You can do sentence = apple + str(x+y)
, print(f'{apple} {x+y}')
, or print('{}{}'.format(apple, x+y)