>>> x = 1234
>>> print "x:", x
x: 1234 >>> x = 1234
>>> print "x:", x
x: 1234 >>> print "x: "+x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
To my mind, "x: "+x is ugly. Perl style "x: $x" makes more sense. But I don't speak python, so my opinion is meaningless.Python 2.6 introduces string interpolation like Ruby/Perl. (Older versions used printf-style format strings.)
print "x: {x}".format(x=1234) # New in 2.6
print "x: %d" % x # Old
http://docs.python.org/tutorial/inputoutput.html#fancier-out... >>> "X: %(x)s" % {"x":10}
'X: 10'In perl, "$x" pulls the value of $x from the variable namespace. In your Py2.6 example, the "{x}" format in the string has nothing to do with a variable named x; rather it references the keyword args supplied to format.
To simulate perl's
print "x: $x";
you would print "x: {x}".format(x=x)