I'm not sure if this is the best way, or if it will work in all cases, but I'll assume you have the template text in a string, either because you've created it with a string or your program has read the source template into a string.
I would use the regular expression library, re
>>> import re
>>> template = "{% block body %} This is x.foo: {{ x.foo }} {% endblock %}"
>>> expr = "\{\{.*x.*\}\}"
>>> result = re.search(expr, template)
>>> try:
>>> print result.group(0)
>>> except IndexError:
>>> print "Variable not used"
The result will be:
'{{ x.foo }}'
or throw the exception I caught:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: no such group
which will print "Variable not used"