Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having issues with turning integers to strings and then concatenating

Tags:

python

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?

like image 317
Absolutechad Avatar asked Jan 27 '26 02:01

Absolutechad


1 Answers

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)
like image 60
Jasmijn Avatar answered Jan 29 '26 14:01

Jasmijn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!