why is this variable not printing out properly? - 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: why is this variable not printing out properly? (/thread-1355.html) Pages:
1
2
|
why is this variable not printing out properly? - dursland - Dec-27-2016 Using python3 and am new - learning via introtopython.org and I'm having a problem. var1 = (2+2) print('The result of all calculations is ' + var1 + ' and we are done.')Which returns the error of Quote:Traceback (most recent call last): What am I doing wrong? When I change the variable to a string the line executes properly. RE: why is this variable not printing out properly? - Mekire - Dec-27-2016 (Dec-27-2016, 07:00 AM)dursland Wrote: When I change the variable to a string the line executes properly.That is exactly it. You can't concatenate a string and int. The recommended way would be to use format. String concatenation in general is not a great technique (though often the first thing taught >.>) var1 = 2 + 2 print('The result of all calculations is {} and we are done.'.format(var1)) RE: why is this variable not printing out properly? - wavic - Dec-27-2016 Hello! Using '+' for a string concatenation works but for strings. You are trying to concatenate a string with an integer without to convert the number into string - str(num) .@Mekire has showed you the proper way RE: why is this variable not printing out properly? - dursland - Dec-27-2016 Thanks. Good to know, I'm following the lessons in order on introtopython. RE: why is this variable not printing out properly? - snippsat - Dec-27-2016 Now that 3.6 is out,also f-string. >>> var1 = 2 + 2 >>> print(f'The result of all calculations is {var1} and we are done.') The result of all calculations is 4 and we are done. RE: why is this variable not printing out properly? - Mekire - Dec-27-2016 (Dec-27-2016, 09:43 AM)snippsat Wrote: Now that 3.6 is out,also f-string.Wow. That is a pretty neat addition. RE: why is this variable not printing out properly? - sixteenornumber - Dec-27-2016 here is one more method. >>> var1 = 2 + 2 >>> print "the result of all calculations is %s and we are done." % var1 the result of all calculations is 4 and we are done.you can also print floats to a specific decimal place. >>> PI = 3.14159265389793 >>> print "Pi to the 2nd decimal place is %.02f" %PI Pi to the 2nd decimal place is 3.14 >>> print "Pi to the 3rd decimal place is \"%.03f\" and pi to the 4th decimal place is \"%.04f\"." %(PI, PI) Pi to the 3rd decimal place is "3.141" and pi to the 4th decimal place is "3.1415". RE: why is this variable not printing out properly? - Mekire - Dec-27-2016 The % method of string formatting should really be considered deprecated (despite Zed's irrelevant opinion). Everything that can be done with it can be done much more cleanly with format .
RE: why is this variable not printing out properly? - samRddhi - Dec-22-2017 Use print('The result of all calculations is ' + str(var1) + ' and we are done.') RE: why is this variable not printing out properly? - IAMK - Apr-08-2018 (Dec-27-2016, 07:16 AM)Mekire Wrote: The recommended way would be to use format. String concatenation in general is not a great technique (though often the first thing taught >.>) Could you please explain why that is? To me, a simple typecast to string seems cleaner. |