How to format a string using a dictionary in Python Last Updated : 23 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, we can use a dictionary to format strings dynamically by replacing placeholders with corresponding values from the dictionary. For example, consider the string "Hello, my name is {name} and I am {age} years old." and the dictionary {'name': 'Alice', 'age': 25}. The task is to format this string using the dictionary to produce "Hello, my name is Alice and I am 25 years old.". Let's explore several ways to achieve this.Using str.format with Double Asterisks (**kwargs)str.format() method allows passing dictionary values as keyword arguments using the ** operator. Python # Input string and dictionary template = "Hello, my name is {name} and I am {age} years old." data = {'name': 'Alice', 'age': 25} # Format the string res = template.format(**data) # Resulting string print(res) OutputHello, my name is Alice and I am 25 years old. Explanation:** operator unpacks the dictionary into keyword arguments.str.format() method replaces placeholders in the string with corresponding dictionary values.Let's explore some more ways and see how we can format a string using a dictionary in Python.Table of ContentUsing str.format_mapUsing F-strings with VariablesUsing Template Strings from string ModuleUsing str.format_mapstr.format_map() method directly formats the string using a dictionary without unpacking it. Python # Input string and dictionary template = "Hello, my name is {name} and I am {age} years old." data = {'name': 'Alice', 'age': 25} # Format the string res = template.format_map(data) # Resulting string print(res) OutputHello, my name is Alice and I am 25 years old. Explanation:str.format_map() method uses the dictionary directly for formatting.It is more concise than str.format() when working with dictionaries.Using F-strings with VariablesFor simple cases, we can use f-strings in combination with variable unpacking. Python # Input dictionary data = {'name': 'Alice', 'age': 25} # Format the string using f-strings res = f"Hello, my name is {data['name']} and I am {data['age']} years old." # Resulting string print(res) OutputHello, my name is Alice and I am 25 years old. Explanation:F-strings allow embedding expressions directly inside string literals.We can access dictionary values using data['key'] syntax within the f-string.Using Template Strings from string ModuleThe string.Template class provides another way to format strings using $ placeholders. Python from string import Template # Input string and dictionary template = Template("Hello, my name is $name and I am $age years old.") data = {'name': 'Alice', 'age': 25} # Format the string res = template.substitute(data) # Resulting string print(res) OutputHello, my name is Alice and I am 25 years old. Explanation:Template class uses $key placeholders to insert dictionary values.substitute method replaces placeholders with corresponding dictionary values. Comment More infoAdvertise with us Next Article Convert String Dictionary to Dictionary in Python K Kanchan_Ray Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads How to Store Values in Dictionary in Python Using For Loop In this article, we will explore the process of storing values in a dictionary in Python using a for loop. As we know, combining dictionaries with for loops is a potent technique in Python, allowing iteration over keys, values, or both. This discussion delves into the fundamentals of Python dictiona 3 min read Convert String Dictionary to Dictionary in Python The goal here is to convert a string that represents a dictionary into an actual Python dictionary object. For example, you might have a string like "{'a': 1, 'b': 2}" and want to convert it into the Python dictionary {'a': 1, 'b': 2}. Let's understand the different methods to do this efficiently.Us 2 min read How to Print a Dictionary in Python Python Dictionaries are the form of data structures that allow us to store and retrieve the key-value pairs properly. While working with dictionaries, it is important to print the contents of the dictionary for analysis or debugging.Example: Using print FunctionPython# input dictionary input_dict = 3 min read How to change any data type into a String in Python? In Python, it's common to convert various data types into strings for display or logging purposes. In this article, we will discuss How to change any data type into a string. Using str() Functionstr() function is used to convert most Python data types into a human-readable string format. It is the m 2 min read Convert Dictionary to String List in Python The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st 3 min read Python - Convert key-value String to dictionary Sometimes, while working with Python strings, we can have problems in which we need to convert a string's key-value pairs to the dictionary. This can have applications in which we are working with string data that needs to be converted. Let's discuss certain ways in which this task can be performed. 4 min read Like