Mastering String Formatting in Python
String formatting is a fundamental skill in Python, allowing you to create dynamic and readable strings by embedding variables and expressions within text. This challenge will test your understanding of various string formatting techniques, ensuring you can effectively construct strings for different scenarios. It's crucial for tasks like generating reports, creating user interfaces, and logging data.
Problem Description
You are tasked with implementing a function called format_string that takes a format string and a dictionary of values as input. The function should return a new string where the placeholders in the format string are replaced with the corresponding values from the dictionary. The format string will use the following placeholders:
{name}: Replaced with the value associated with the key "name" in the dictionary.{age}: Replaced with the value associated with the key "age" in the dictionary.{city}: Replaced with the value associated with the key "city" in the dictionary.{occupation}: Replaced with the value associated with the key "occupation" in the dictionary.
If a placeholder is encountered in the format string but the corresponding key is not present in the dictionary, the placeholder should remain unchanged in the output string.
Key Requirements:
- The function must handle missing keys gracefully.
- The function must correctly replace all specified placeholders with their corresponding values.
- The function should return a new string; it should not modify the original format string.
Expected Behavior:
The function should return a string with the placeholders replaced by the values from the dictionary. If a placeholder is not found in the dictionary, it should remain as is.
Edge Cases to Consider:
- Empty dictionary.
- Format string with no placeholders.
- Format string with placeholders not in the dictionary.
- Format string with multiple occurrences of the same placeholder.
- Dictionary with values of different data types (strings, integers, floats, etc.).
Examples
Example 1:
Input: format_string("{name} is {age} years old and lives in {city}.", {"name": "Alice", "age": 30, "city": "New York"})
Output: Alice is 30 years old and lives in New York.
Explanation: All placeholders are present in the dictionary and are replaced correctly.
Example 2:
Input: format_string("Hello, {name}! You are {age} years old.", {"name": "Bob"})
Output: Hello, Bob! You are {age} years old.
Explanation: The "age" placeholder is not found in the dictionary, so it remains unchanged.
Example 3:
Input: format_string("{occupation} is a great profession.", {"name": "Charlie", "age": 25, "city": "London", "occupation": "Software Engineer"})
Output: Software Engineer is a great profession.
Explanation: All placeholders are present and replaced.
Example 4:
Input: format_string("My name is {name}.", {})
Output: My name is {name}.
Explanation: The dictionary is empty, so all placeholders remain unchanged.
Constraints
- The format string will be a string.
- The dictionary will be a dictionary with string keys and any type of values.
- The length of the format string can be up to 200 characters.
- The dictionary can contain up to 10 key-value pairs.
- The function must execute in O(n) time, where n is the length of the format string.
Notes
Consider using string replacement or the str.format() method to implement the formatting. Think about how to handle cases where a placeholder is not found in the dictionary without raising an error. Focus on creating a robust and efficient solution.