Normally, i can use the following code to implement a variable within a string
print "this is a test %s" % (test)
however, it doesn't seem to work as i had to use this
from __future__ import print_function
>>> test = '!'
>>> print "this is a test %s" % (test)
this is a test !
If you import print_function feature, print acts as function:
>>> from __future__ import print_function
>>> print "this is a test %s" % (test)
File "<stdin>", line 1
print "this is a test %s" % (test)
^
SyntaxError: invalid syntax
You should use function call form after the import.
>>> print("this is a test %s" % (test))
this is a test !
SIDE NOTE
According to the documentation:
str.formatis new standard in Python 3, and should be preferred to the%formatting.
>>> print("this is a test {}".format(test))
this is a test !
str.format. Thank you for comment.Try this:
print("this is a test", test)
Or this:
print("this is a test {}".format(test))
from __future__ import print_function, you now replace the print statement with its function counterpart. Thus, simply print some_value % (formatter) doesn't work. See the answers for what works.If you're implementing a string use %s.
test?