Sorry. I am not trying to start any flame. My scripting experience is from Perl, and I am pretty new in Python.
I just want to check whether I can have the same degree of flexibility as in Python.
In Python :
page = form.getvalue("page")
str = 'This is string : ' + str(int(page) + 1)
In Perl :
$str = 'This is string : ' . ($page + 1);
Is there any way I can avoid int / str conversion?
No, since Python is strongly typed. If you keep page
as an int you can do the following:
s = 'This is string : %d' % (page + 1,)
You could use:
mystr = "This string is: %s" % (int(page) + 1)
... the string conversion will be automatic when interpolating into the %s
via the %
(string formating operator).
You can't get around the need to convert from string to integer. Python will never conflate strings for other data types. In various contexts Python can return the string or "representation" of an object so there are some implicit data casts into string forms. (Under the hood these call .__str__()
or .__repr__()
object methods).
(While some folks don't like it I personally think the notion of overloading %
for string interpolation is far more sensible than a function named sprintf()
(if you have a language with operator overloading support anyway).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With