ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
from string import Template
print(Template('$a-$b').substitute(a=1, b=2), Template('$a$c').safe_substitute(a='x'), Template('${a}bc').substitute(a='x'))
  1. A. 1-2 x$c xbc
  2. B. 1-2 xc xbc
  3. C. 1-2 x$c x$bc
  4. D. 12 x$c xbc

Correct answer: A. 1-2 x$c xbc

substitute fills $a and $b, and the braces in ${a} end the name before bc. safe_substitute leaves $c unchanged when no value is given, rather than raising KeyError.

More Python Programming questions

Exams that ask this topic