Python Forum
Convert integer to string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Convert integer to string (/thread-4516.html)



Convert integer to string - Maigol - Aug-22-2017

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.


RE: Convert integer to string - ichabod801 - Aug-22-2017

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.


RE: Convert integer to string - Maigol - Aug-22-2017

(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")



RE: Convert integer to string - Jafinoxal - Aug-22-2017

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.


RE: Convert integer to string - Maigol - Aug-22-2017

(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!


RE: Convert integer to string - python_master89 - Mar-05-2018

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