3

I have an integer called score, I turn it into a string and then try to get python to print a string of score plus a few other strings

score = 0
str(score)
variable = (" you have" + score + "points")
print(variable)

However it just tells me that score is still an integer, how do I fix this?

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

3

Strings and numbers are immutable, so str and int don't change the underlying value, but instead return a new value that you have to use.

For example:

score = 0
score = str(score)
variable = " you have " + score + " points"
print(variable)

or

score = 0
variable = " you have " + str(score) + " points"
print(variable)

However, using f-strings is probably better in this situation:

score = 0
variable = f" you have {score} points"
print(variable)
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
0

First of all python is case-sensitive you need to change the names. And then you are doing all conversion and casting in memory because strings are immutable, and values don't change unless they are re-assigned so assign it like this:

Score = 0
Score = str(Score)
Variable = "you have " +Score + " points"
print(Variable)

Output:

you have 0 points
Wasif
  • 14,755
  • 3
  • 14
  • 34