How To Print int Values

Question:

So, I am making a test SCII adventure game and when i need to print out the enemy HP it tells me I can’t print int values with str values.

Code:

enemy_hp=73
print("Enemy HP: " + enemy_hp)

Result:

TypeError: can only concatenate str (not "int") to str

Wanted result:

Enemy HP: 73

Can someone help me out pls.

Asked By: Luka

||

Answers:

print(f'Enemy HP: {enemy_hp}')
Answered By: Aakash Rathee

You can use .format to do it. Such as below:

enemy_hp=73
print("Enemy HP: " + "{ }".format(enemy_hp))
Answered By: Muhammad Affan

This happens because you are concatenating int and strings when printing out
You can solve this in various ways

  1. using a format string
print(f'Enemy HP: {enemy_hp}')
  1. converting int to str
print('Enemy HP: '+ str(enemy_hp))
Answered By: Edmerson Pizarra

str cant + int!
so do this
print(f"Enemy HP: " {enemy_hp})
or do this
print(("Enemy HP: + {}").format(enemy_hp))

Answered By: moonboy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.