[Tutor] Concatenation
avi.e.gross at gmail.com
avi.e.gross at gmail.com
Fri Aug 19 18:40:48 EDT 2022
Others have already given decent answers on a few of the umpteen ways you can combine text and numbers and often other things. It can make lots of sense as some aspects of Python are in-between various extremes of other programming languages.
I am currently re-learning JavaScript which has had oodles of changes that interest me as well as some languages that tend towards the other extreme. Some languages insist you tell them in great detail exactly what you want your variables to contain. If you want to place a number within text, you need to not only convert an aspect of it by calling a function with some name like "str" but often using various functions to detail how many digits past the decimal point you want or whether to show it in scientific notation or have embedded commas or leading zeroes or have it rounded or truncated and so on. You can, of course, do all those things in python too, and even set up various objects to have assorted methods that invisibly are used to make the transformation for you when needed.
JavaScript now goes a bit too far and too inconsistently in another direction and you must be careful. 2 + 3 is 5 but 2 + "3" is '23' but in some contexts it will happily read text and try to make a number, such as this:
2 > "2"
false
2 > "1"
true
2 >= "2"
True
It does so many automatic conversions that sometimes you get really weird results that are hard to debug.
So python has many methods where you should do your own conversions and others where certain assumptions and tricks do much of the work for you. There is a print() family of ways using things like %3.2f much of which has similarities in the f-string like f"With two decimal places: {variable:.2f}"
When in doubt you can make a string first using as many steps to make it into one string with parts converted and THEN printing it is trivial!
-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of Alan Gauld via Tutor
Sent: Friday, August 19, 2022 5:12 PM
To: tutor at python.org
Subject: Re: [Tutor] Concatenation
On 19/08/2022 13:42, Gibson Green via Tutor wrote:
> How can I concatenation a string statement with an integer? For example: Congratulations, you are now 24 years old. Age variables was declared and converted to an integer.
> I have:
> print ( ‘ Congratulations, you are now’ + age+ ‘ years old’)
>
> Had an error about concatenation between integer and strings . Please help. Thanks.
The error is because you are trying to use the plus operation on a string and an integere which is incompatible. You need to conveert the integer to a string using str()
But print already converts arguments to strings for you so you don't need the + sign just use:
print( ‘ Congratulations, you are now ’, age, ‘ years old’)
Or, more generally use a format string as others have suggested.
The format string will work anywhere you need to insert a value into a string. print() only works for display purposes.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list