from string import Template name = "Alice" message = Template("Hello ${name}! How are you?") result = message.substitute(name=name) print(result) # Output: Hello Alice! How are you?
from string import Template data = {"title": "Python Tutorial", "author": "John Doe", "year": 2022} template = Template("$title by $author, published in $year") result = template.substitute(data) print(result) # Output: Python Tutorial by John Doe, published in 2022This example demonstrates how to use a dictionary to pass multiple values to a template string. The `substitute()` method replaces placeholders with corresponding values from the dictionary. Package library: The string Template module is part of Python's standard library, which means it's always available and there's no need to install any additional package or library.