- 2

Concatenationing variables in Python?

x = 42 z = 3.14 print(z+x) is "45.14" and print('z'+'x') is "zx". Is there a way to get the result "3.1442" with variables? Edit: in Python. Edit2: I realised you can just put the numbers into strings, actually that was bothering me. x = "42" z = "3.14" print(z+x)=3.1442

30th Oct 2021, 8:00 AM
Jona Žonta
Jona Žonta - avatar
9 Respuestas
+ 4
Jona Žonta Yes using str function print (str(z) + str(x))
30th Oct 2021, 8:12 AM
A͢J
A͢J - avatar
+ 1
Yes this is possible. But you have not mentioned any language.
30th Oct 2021, 8:03 AM
Kashyap Kumar
Kashyap Kumar - avatar
+ 1
try this x = 42 z = 3.14 print(str(x)+str(z))
30th Oct 2021, 12:24 PM
Shadoff
Shadoff - avatar
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Your original answer was already very helpful because I didn't know about str function yet. Thank you also for the last answer (I'll study it), however to me (and to others who might stumble upon this while also being beginners) it was a revelation when I realised there are two options which both work and how both options work (even if one might be more desired than the other and even if it's inconsistent to use both). n = "Jam" a = "8" b = 9 print(n+" is "+a+" years old. Soon to be "+str(b)+".")
31st Oct 2021, 3:54 PM
Jona Žonta
Jona Žonta - avatar
+ 1
Using f string is better in case of readability. print(f'{name} is {age} years old')
31st Oct 2021, 5:30 PM
Shadoff
Shadoff - avatar
0
Why no one told me Edit2 :)
30th Oct 2021, 8:09 PM
Jona Žonta
Jona Žonta - avatar
0
Jona Žonta If you put anything inside single quotes or double quotes that would be String not number.
30th Oct 2021, 8:15 PM
A͢J
A͢J - avatar
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Yes but both can be variables? and that was my question ...
31st Oct 2021, 2:15 PM
Jona Žonta
Jona Žonta - avatar
0
Jona Žonta Which stores value that's called as a variable. So here x and z are variable which can store any type of value like integer, string, float x = 42 #this is integer print (type(x)) # <class 'int'> x = "42" #this is string print (type(x)) # <class 'str'> x = 3.14 #this is float print (type (x)) # <class 'float'>
31st Oct 2021, 3:34 PM
A͢J
A͢J - avatar