[Tutor] Concatenation
dn
PythonList at DancesWithMice.info
Fri Aug 19 18:13:46 EDT 2022
On 20/08/2022 09.12, Alan Gauld via Tutor wrote:
> 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.
There are apparently two questions here.
> 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()
The first question: type-casting or coercion
(https://code.tutsplus.com/articles/the-beginners-guide-to-type-coercion-what-is-coercion--cms-21917)
appears to be understood, and the real-question is merely: 'how is it
done in Python?'.
(presumably you've (OP) programmed in other languages and recently
promoted yourself to Python)
Well done!
> 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’)
The example appeared to ask about (only) printing, "but wait! there's
more!".
BTW remove the inner spaces from the string-literals - print() inserts
spaces as "separators" between multiple-arguments (by default).
> 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.
+1
There are plenty of other situations when one might want to use a
mixed-type expression.
At the risk of charging way-beyond where you (OP) might be today, but
for future reference - and for the others on the list, who are also
interested in F-strings; here's your (western-world) weekend with Python:
What if the output line was more complicated?
eg “3KG of Apples @3.50 per KG cost $10.50”
(spec drawn from a recent tutorial)
NB the 3, 3.5, and 10.5 are numeric-values, and "Apples" is a
string-variable.
(please feel free to substitute currency-symbol (and number of
decimal-places), etc, to suit your locale)
Most of it can be achieved with a simple print() call, but the
symbol-prefixes and -suffix, and the decimal formatting, demand
different types of 'more'. Hence the sage-advice to look at F-strings
and "the Python Format Specification Mini-Language"!
It's a hassle typing a flurry of quotation-marks, parentheses, braces,
colons, and stuff - but even this grey-beard has gradually persuaded his
fingers to work that way!
Web refs (lifted straight from the aforementioned tutorial):
Python’s f-strings or “formatted string literals” were proposed in PEP
498 – Literal String Interpolation https://peps.python.org/pep-0498/
They were introduced in Python 3.6
https://docs.python.org/3/whatsnew/3.6.html
If you’ve not met them before, there’s a good article “Python f-strings:
Everything you need to know!” at https://datagy.io/python-f-strings/
The Format Specification Mini-Language is defined in
https://docs.python.org/3/library/string.html#format-string-syntax
Technical definition is 2.4.3. Formatted string literals
https://docs.python.org/3/reference/lexical_analysis.html#f-strings
More powerful than you might think https://martinheinz.dev/blog/70
--
Regards,
=dn
More information about the Tutor
mailing list