Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert integer to string
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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")
Reply
#4
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.
Reply
#5
(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!
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,815 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  how to convert tuple value into string mg24 2 2,237 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,421 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,230 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,395 Feb-13-2022, 04:50 PM
Last Post: menator01
  How to convert 4 bytes to an integer ? GiggsB 11 6,597 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  Convert string to path using Python 2.7 tester_V 10 6,274 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,169 Jun-16-2021, 02:11 AM
Last Post: tester_V
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,490 May-24-2021, 08:48 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020