2 ms·
It works if you use a comma, like the comment you replied to. Python 2.6 introduces string interpolation like Ruby/Perl. (Older versions used printf-style for
by mbrubeck 18y ago
It works if you use a comma, like the comment you replied to.
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-output-formatting http://docs.python.org/tutorial/inputoutput.html#fancier-out...
- eru 18y agoThis also works: >>> "X: %(x)s" % {"x":10} 'X: 10'
- iclelland 18y agoTo be fair, that's not quite the same as perl/shell-style interpolation. 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)