Templates help you write prompts once and use them many times. This saves time and keeps your prompts clear and organized.
When you want to ask similar questions but with different details.
When you need to keep your prompt style consistent across many uses.
When you want to quickly change parts of a prompt without rewriting everything.
When working in a team to share standard prompts easily.
When automating tasks that require repeated prompt formats.
Syntax
LangChain
from langchain.prompts import PromptTemplate
template = PromptTemplate(
input_variables=["name"],
template="Hello, {name}! How can I help you today?"
)
prompt_text = template.format(name="Alice")
input_variables are placeholders you fill later.
Use {variable_name} inside the template string to mark where values go.
Examples
This template asks about weather in different cities by changing the city name.
LangChain
template = PromptTemplate(
input_variables=["city"],
template="Tell me about the weather in {city}."
)
prompt_text = template.format(city="Paris")
This template uses two variables to create a sentence about a product and its price.
This program shows how one template can greet different users by changing the name each time.
LangChain
from langchain.prompts import PromptTemplate
# Create a template with one variable
template = PromptTemplate(
input_variables=["user_name"],
template="Welcome, {user_name}! Ready to start your journey?"
)
# Use the template with different namesprint(template.format(user_name="Sam"))
print(template.format(user_name="Lily"))
OutputSuccess
Important Notes
Templates keep your prompts neat and easy to update.
Always name your variables clearly to avoid confusion.
You can reuse one template many times with different inputs.
Summary
Templates let you write prompts once and reuse them easily.
They use placeholders to insert different information each time.
This helps save time and keeps your prompts consistent.
Practice
(1/5)
1. Why do templates help when creating prompts in Langchain?
easy
A. They make prompts run faster by skipping processing
B. They automatically generate new prompts without any input
C. They let you reuse the same prompt structure with different data
D. They replace the need for any user input
Solution
Step 1: Understand what templates do
Templates use placeholders to create a prompt structure that can be filled with different values.
Step 2: Recognize the benefit of reusing prompts
This means you write the prompt once and reuse it many times with different data, saving time and keeping consistency.
Final Answer:
They let you reuse the same prompt structure with different data -> Option C
Quick Check:
Reusable prompt structure = D [OK]
Hint: Templates reuse prompt text with placeholders [OK]
Common Mistakes:
Thinking templates generate prompts without input
Believing templates remove need for user input
Assuming templates speed up prompt execution
2. Which of the following is the correct way to define a prompt template with a placeholder named name in Langchain?
easy
A. PromptTemplate(template="Hello, %name%!")
B. PromptTemplate(template="Hello, $name!")
C. PromptTemplate(template="Hello, <name>!")
D. PromptTemplate(template="Hello, {name}!")
Solution
Step 1: Recall Langchain placeholder syntax
Langchain uses curly braces {} to mark placeholders in prompt templates.
Step 2: Match the correct syntax
The correct syntax for a placeholder named 'name' is {name}, so the template string should be "Hello, {name}!".
Final Answer:
PromptTemplate(template="Hello, {name}!") -> Option D
Quick Check:
Curly braces for placeholders = A [OK]
Hint: Use curly braces {} for placeholders in templates [OK]
Common Mistakes:
Using $ or % instead of curly braces
Using angle brackets <> which are invalid
Forgetting to wrap the template string in quotes
3. Given the following code snippet, what will be the output?
A. The placeholder name in template and format do not match
B. The template string is missing curly braces
C. The format method is not supported in PromptTemplate
D. The import statement is incorrect
Solution
Step 1: Compare placeholder and format argument names
The template uses {user} but the format call uses username="Bob" which does not match.
Step 2: Understand placeholder replacement rules
Since the placeholder {user} is not provided a value, formatting will fail or leave it unchanged.
Final Answer:
The placeholder name in template and format do not match -> Option A
Quick Check:
Placeholder and argument names must match = A [OK]
Hint: Match placeholder names exactly in format() call [OK]
Common Mistakes:
Using different names for placeholders and values
Forgetting curly braces in template
Assuming format() is unsupported
5. You want to create a reusable prompt template that asks for a user's favorite color and hobby. Which approach best uses templates to keep your prompts consistent and easy to update?
hard
A. Use separate templates for color and hobby and combine them manually
B. Create a template with placeholders {color} and {hobby}, then fill them each time you ask
C. Write a new prompt string every time with the user's answers included
D. Hardcode the questions and ignore user input for simplicity
Solution
Step 1: Identify the goal of reusability and consistency
Using one template with placeholders for both color and hobby lets you reuse the prompt easily and keep it consistent.
Step 2: Compare options for maintainability
Writing new strings each time or splitting templates adds complexity and risks inconsistency.
Final Answer:
Create a template with placeholders {color} and {hobby}, then fill them each time you ask -> Option B
Quick Check:
Single template with placeholders = C [OK]
Hint: Use one template with multiple placeholders for related data [OK]
Common Mistakes:
Writing new prompt strings every time
Splitting related questions into separate templates
Ignoring user input to simplify prompts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.