Python Forum

Full Version: Convert integer to string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm new to python and I need to convert the content of a variable (which is an integer) to a string.
I tried with "str(name_of_the_variable)", but didn't work.
How did it not work? That's the correct way to do it, so without the error message we can't tell you what is wrong.
(Aug-22-2017, 08:13 PM)ichabod801 Wrote: [ -> ]How did it not work? That's the correct way to do it, so without the error message we can't tell you what is wrong.

Sorry, this is what it says when I get to a part of the code:

Traceback (most recent call last):
File "Regla de 3 directa.py", line 10, in <module>
print("Va a ser igual a " + Xstr)
TypeError: must be str, not float

this is the code, by the way

Propor = (input("Haces regla de 3 directa o inversamente proporcional? [D/I]"))
A = int(input("Si: "))
B = int(input("Es igual a: "))
C = int(input("Entonces: "))
if Propor == "D" or "I":
	if Propor == "D":
		X = C * B / A
		Xstr = X
		str(Xstr)
		print("Va a ser igual a " + Xstr)
	elif Propor == "I":
		X = B * A / C
		Xstr = X
		print("Va a ser igual a " + Xstr)
print("Programa Terminado")
Instead of str(Xstr) you need Xstr = str(Xstr) on line 9.
You were calling the str function but not assigning the result to anything.
(Aug-22-2017, 08:25 PM)Jafinoxal Wrote: [ -> ]Instead of str(Xstr) you need Xstr = str(Xstr) on line 9.
You were calling the str function but not assigning the result to anything.

It worked! :D Thank you so much!
To convert an integer to string you have to use the builtin function "str"

just use str(integer_number) and it will return a string converted.

If you want check here more details about how to conver integer to string