[Tutor] Concatenation

dn PythonList at DancesWithMice.info
Fri Aug 19 20:09:39 EDT 2022


On 20/08/2022 10.40, avi.e.gross at gmail.com wrote:
> 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.
...
> 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!

(with due apologies to OP for rabbit-spelunking)


Whilst shuddering at the mention of JavaScript, it did occur to me that
@Alan had mentioned the simple answer - use str()! On top of which
intruded @Avi's thoughts about the many and various methods that might
be applied to integers.

Remember: in Python int[eger]s are a class rather than a "primitive" type!


So, I pulled-out my trusty REPL and tried:

    help( int )

Was surprised to see that neither str() nor __str__() is mentioned.

Yet __repr__() is there! Many other standard type-conversions are
listed, eg bool() and float(). Even, to_bytes() and from_bytes() are
listed, and presumably have been, going back into the mists of time...

Wonder why, when Python 3 (-ish) took strings from ASCII-bytes to UTF-8,
no string equivalents were (formally) added?

-- 
Regards =dn


More information about the Tutor mailing list