- 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
9 Respostas
+ 4
Jona Žonta
Yes using str function
print (str(z) + str(x))
+ 1
Yes this is possible. But you have not mentioned any language.
+ 1
try this
x = 42
z = 3.14
print(str(x)+str(z))
+ 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)+".")
+ 1
Using f string is better in case of readability.
print(f'{name} is {age} years old')
0
Why no one told me Edit2 :)
0
Jona Žonta
If you put anything inside single quotes or double quotes that would be String not number.
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Yes but both can be variables? and that was my question ...
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'>