String Formatter
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:
name = "john doe", age = 25, score = 95.5
{'greeting': 'Hello, John Doe!', 'info': 'John Doe is 25 years old', 'score_display': 'Score: 95.50'}.title() capitalizes 'john doe' to 'John Doe', :.2f formats float to 2 decimals
Constraints:
- Use f-strings or .format()
- Apply .title() for proper name capitalization
String Formatting in Python: Background Knowledge
Core Concepts
Python provides multiple mechanisms for string formatting, each with distinct use cases and syntax. F-strings (formatted string literals), introduced in Python 3.6, are the modern standard and offer the best readability and performance. They allow you to embed expressions directly within strings using curly braces: f"text {expression}". The expression inside the braces is evaluated at runtime and converted to a string.
String methods like .upper(), .lower(), and .title() transform the case of characters in a string. The .title() method capitalizes the first letter of each word, which is particularly useful for formatting names. These methods return new strings without modifying the original (strings are immutable in Python).
Format specifiers allow you to control how values are displayed. Within f-strings, you can use a colon followed by format specifications: f"{value:specification}". For example, f"{score:.2f}" formats a number with exactly 2 decimal places. This is essential when displaying monetary values, percentages, or other numeric data that requires precision.
Why This Matters
Understanding string formatting is fundamental because it bridges raw data and human-readable output. In real applications, you'll constantly need to present data in specific formats—whether displaying user information, generating reports, or creating API responses. Mastering these techniques makes your code cleaner and more maintainable.
Algorithm/Approach
The solution follows a straightforward data transformation and formatting pattern:
- Transform input data using string methods (convert name to title case)
- Format output values using f-strings with appropriate format specifiers
- Aggregate results into the required data structure (dictionary)
This is a simple mapping problem: take raw inputs, apply formatting rules, and return structured output.
Step-by-Step Strategy
Step 1: Understand the input requirements
- Identify what each parameter represents and its expected type
- Note that name comes in lowercase and needs transformation
Step 2: Process the name
- Apply the .title() method to convert "john doe" → "John Doe"
- Store this processed name for reuse in multiple output fields
Step 3: Create each dictionary entry
- greeting: Use an f-string with the title-cased name
- info: Use an f-string combining the processed name with age
- score_display: Use an f-string with the .2f format specifier to display exactly 2 decimal places
Step 4: Return the dictionary
- Ensure all three keys are present with correctly formatted values
Step 5: Test with the sample
- Verify your output matches the expected result exactly
Common Pitfalls
- Forgetting to apply .title(): Using the raw name input will produce incorrect output
- Incorrect format specifier syntax: Remember the colon comes before the format spec (: not =), and .2f means 2 decimal places for floats
- Not reusing the processed name: Applying .title() separately for each field is inefficient; store it once
- Type mismatches: Ensure age is treated as an integer and score as a float in your format specifiers
- Dictionary key typos: Match the exact key names specified: "greeting", "info", "score_display"
- Spacing and punctuation: Pay attention to exact formatting—"Hello, " includes a comma and space
Time & Space Complexity
- Time Complexity: O(n) where n is the length of the input strings. String methods like .title() and f-string formatting must process each character, and dictionary creation is O(1) for a fixed number of keys.
- Space Complexity: O(n) for storing the output strings. The dictionary itself uses constant space, but the strings it contains scale with input length.
In practice, for typical name and numeric inputs, these operations are negligible and execute in microseconds.