v9.1.1
Menu

String Formatter

easyFREEVariables & TypesPython StringsPython Foundations

Problem Statement

Format strings using f-strings and string methods.

Background

Python offers multiple ways to format strings:

  • f-strings: f"Hello {name}"
  • .format(): "Hello {}".format(name)
  • String methods: .upper(), .lower(), .title(), .strip()

Your Task

Write a function format_info(name, age, score) that returns a formatted info card.

Output Format

Return a dictionary with:

  • "greeting": "Hello, {name}!" (name in title case)
  • "info": "{name} is {age} years old"
  • "score_display": "Score: {score:.2f}" (2 decimal places)

Example

Input
name = "john doe", age = 25, score = 95.5
Output
{'greeting': 'Hello, John Doe!', 'info': 'John Doe is 25 years old', 'score_display': 'Score: 95.50'}
Reasoning
  • Inputs: name is a lowercase string "john doe" made of two words, age is the whole number 2525, and score is the decimal number 95.595.5.
  • Name in title case: each word gets its first letter capitalised and the remaining letters lowercased, so "john" becomes "John" and "doe" becomes "Doe", giving "John Doe". The same cased name is reused in both greeting and info.
  • Greeting and info: the cased name drops into the two templates, producing Hello, John Doe! and John Doe is 25 years old. The age appears exactly as given, with no decimal point.
  • Score: 95.595.5 has only one digit after the decimal point, so it is padded with a trailing zero to show exactly two decimal places: 95.5095.50, giving Score: 95.50.
  • Output: a dictionary with the three keys greeting, info and score_display, each holding a string.
  • Notice that title case also lowers letters that are already capitalised: an all-caps "ALICE" becomes "Alice". Likewise, a score with more digits, such as 88.12388.123, is rounded to two places (88.1288.12) rather than padded.

Constraints

  • Use f-strings or .format()
  • Apply .title() for proper name capitalization

Learn this in

More from Python Foundations

solution.py

Test results

Sign in to run your code against the test cases.