← Back to Blog

DEF Mini Project — Banner

The problem...

You want your output to look clean. Structured. Like it was built with intention.

Right now you'd write the same formatting code every time you need a title. Copy. Paste. Repeat.

One function fixes that.

The idea!

A banner function takes a title and wraps it in a border. Call it once — clean output. Call it ten times — still one line each.

Your mission

Write a function called banner() that prints a title surrounded by a border of = characters.

The solution

def banner():
    title = input("Enter a title: ")
    border = "=" * len(title)
    print(border)
    print(title)
    print(border)

banner()
banner()
banner()

Test it

# Enter a title: RedHorn
# =======
# RedHorn
# =======

# Enter a title: Control Flow
# ============
# Control Flow
# ============

What's really happening

"=" * len(title) creates a string of = characters exactly as long as the title. The border fits perfectly — no matter what the user types.

Three calls. Three banners. One function definition. That's the point.

Go further

  • Change the border character — use *, -, or # instead of =
  • Add padding — print a blank line before and after the banner
  • Add side borders — | RedHorn | instead of just RedHorn

What you should understand now

  • A function groups related code under one name
  • Call it once — or a hundred times — the code stays in one place
  • "=" * n repeats a string n times — useful for formatting
  • Functions make output consistent — the same banner, every time
[ login to bookmark ] // copied! 24 views · 1 min
// resources
Exercise banner.py
← prev Your first function next → Give your function something to work with
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.