+ 1

How do you convert float to string using def?

I found out that if you say something like: user=float(5) #user is a float s=(user) + (2) #equation solved as float user=str(user) #user is a string w=(user) + (" is a number") #equation solved as string You essentially converted a float into a string. So in between those two lines you could write an equation and it would then solve as a float then after you converted it into a string you could write an equation and it solve it as a string. So my question is how do you do that as a def statement? I tried: user=float(12) #user is a float def convert(x): #variable is supposed to convert into string x=str(x) user(convert) #thus user should be converted into a string. But it doesn't convert into a string, it is still defined as a float. Is there a way to convert a variable into a string using def?

25th Aug 2016, 9:44 PM
Darth Vador
Darth Vador - avatar
4 odpowiedzi
+ 5
your code should be like this user = float(12) def convert(x): x=str(x) return x user = convert(user)
25th Aug 2016, 10:39 PM
Mahendra Kamble
Mahendra Kamble - avatar
+ 3
Mahendra is correct, but why you need a def to convert when you can use w=str(user) + " is a number" ?
26th Aug 2016, 3:59 AM
Eduard Hirsch
Eduard Hirsch - avatar
0
it seems you meant convert(users) instead of users(convert). The one issue i can think of is the namespace. Because the conversion happens inside the def function, if you use x, it would give you a string, but if you use 'user' it will return a float.
25th Aug 2016, 9:59 PM
Shashi Shekhar
0
I have a bunch of formal floats that need to be converted into strings for various reasons. Thank you Mahendra!
26th Aug 2016, 4:02 AM
Darth Vador
Darth Vador - avatar