⚡0 XP🔥0

Reusable Prompt Templates with Python

Lesson 16 of 19 ⏱ 4 min read ✎ 3 exercises ❓ 4-question quiz ▶ Runs Python

Turn your best prompts into reusable templates with placeholders, validate inputs, generate prompts in bulk from CSV data, and keep a versioned prompt library.

Why templates?#

Once you've written a prompt that works well, you'll want to use it again - for another student, product, customer or topic. Retyping it each time causes mistakes and inconsistent results. A prompt template is the proven prompt with placeholders for the parts that change.

Explain {topic} to {level} students using {num_examples} real-life
Indian examples. Keep it under {words} words. End with one practice question.
हिंदी में समझें

Prompt template = एक अच्छा, जाँचा-परखा prompt जिसमें बदलने वाली जगहों पर खाली खाने (placeholders) होते हैं - जैसे {topic}, {level}।

यह एक छपे हुए form जैसा है: form एक ही रहता है, बस हर बार नाम-पता अलग भरा जाता है। इससे हर बार prompt की quality एक जैसी रहती है, गलतियाँ कम होती हैं, और Python से आप एक साथ सैकड़ों prompts बना सकते हैं (जैसे हर student के लिए अलग message)।

Templates with f-strings and .format()#

▶ Try it YourselfPython
Ctrl + Enter to run

str.format() is ideal for templates stored as text (in files or a database) because the placeholders stay as {name} until you fill them.

Validating inputs#

A missing value causes a KeyError; an empty value creates a broken prompt. Check before sending:

▶ Try it YourselfPython
Ctrl + Enter to run

Bulk generation from data#

Generate one prompt per row of a spreadsheet - personalised messages, product descriptions, report comments:

▶ Try it YourselfPython
Ctrl + Enter to run

In the next lesson you'll send each of these prompts to an AI API automatically and save the replies to a file.

Storing templates#

Keep templates outside your code so non-programmers can edit them:

{
  "progress_comment": {
    "version": 3,
    "description": "Teacher comment for report cards",
    "template": "Write a short, encouraging progress comment ... {name} ... {score}",
    "temperature": 0.5
  },
  "fee_reminder": {
    "version": 1,
    "description": "Polite fee reminder SMS",
    "template": "Write a {tone} reminder to {name} ..."
  }
}
▶ Try it YourselfPython
Ctrl + Enter to run

Template best practices#

  1. Start from a tested prompt - only templatise prompts that already work well.
  2. Name placeholders clearly: {student_name}, not {x}.
  3. Validate all inputs; strip extra spaces.
  4. Version important templates and note what changed.
  5. Escape literal braces - if your template needs real { } (e.g. JSON examples), write {{ }}.
  6. Keep a test set of inputs to re-check templates after edits.
T = 'Return JSON like {{"name": "...", "score": 0}} for student {student}.'
print(T.format(student="Asha"))
# Return JSON like {"name": "...", "score": 0} for student Asha.

Summary#

  • Templates = proven prompts with {placeholders}, filled with str.format().
  • Validate inputs: missing and empty values break prompts.
  • Generate prompts in bulk from CSV/Excel data.
  • Store templates in JSON with versions; use {{ }} for literal braces.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Fill a template safely

Write fill(template, **values) that returns the template filled using str.format. If any placeholder is missing, raise ValueError with the message Missing value: <name>.

2

Find the placeholders

Write placeholders(template) that returns a sorted list of unique placeholder names in a template, using string.Formatter().parse().

3

Bulk prompts from CSV

Using the template and CSV text in the starter, print one prompt per row (use csv.DictReader).

Expected output:
Write a 2-line birthday wish for Asha, a Python student, mentioning cricket.
Write a 2-line birthday wish for Ravi, a Prompt Engineering student, mentioning music.

Quick Quiz

Q1.

What is a prompt template?

Q2.

What does "Hello {name}".format(name="Asha") return?

Q3.

Why validate template inputs before sending?

Q4.

Why keep version numbers on important templates?