Lesson guide
What this Python exercise practices
Format a String is a beginner practice lesson that focuses on strings, formatting, traversal. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- String values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Related public exercises
Summary
Combine text and values into a single formatted string using Python strings.
Problem statement
Implement format_greeting(name, age) that returns a greeting sentence exactly in this format: Hello, {name}! You are {age} years old. The function should convert the age to text if necessary and insert the provided name and age into the template. Do not print; return the formatted string.
Task
Practice converting values to strings and using string formatting to produce a readable message.
Examples
Basic example
Input
format_greeting('Alice', 30)
Output
Hello, Alice! You are 30 years old.
Explanation
Name and numeric age are inserted into the template.
Input format
Two arguments: name (string) and age (any value convertible to string).
Output format
A single string matching the greeting template exactly.
Constraints
Return a string. Preserve the input values as-is when converting (no rounding unless Python's default str() does it).
Samples
Sample input 0
format_greeting('Bob', '25')
Sample output 0
Hello, Bob! You are 25 years old.
Explanation 0
Age provided as a string is placed into the template unchanged.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.