How to format a string using a dictionary in Python Last Updated : 23 Jan, 2025 Comments Improve Suggest changes 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 How to format a string using a dictionary in Python K Kanchan_Ray Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads How to Add User Input To A Dictionary - Python The task of adding user input to a dictionary in Python involves taking dynamic data from the user and storing it in a dictionary as key-value pairs. Since dictionaries preserve the order of insertion, we can easily add new entries based on user input.For instance, if a user inputs "name" as the key 3 min read 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 How to Initialize a Dictionary in Python Using For Loop When you want to create a dictionary with the initial key-value pairs or when you should transform an existing iterable, such as the list into it. You use string for loop initialization. In this article, we will see the initialization procedure of a dictionary using a for loop. Initialize Python Dic 3 min read How to copy a string in Python Creating a copy of a string is useful when we need a duplicate of a string to work with while keeping the original string intact, strings in Python are immutable which means they can't be altered after creation, so creating a copy sometimes becomes a necessity for specific use cases.Using SlicingSli 2 min read How to Create String Array in Python ? To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values 2 min read How to Update a Dictionary in Python This article explores updating dictionaries in Python, where keys of any type map to values, focusing on various methods to modify key-value pairs in this versatile data structure. Update a Dictionary in PythonBelow, are the approaches to Update a Dictionary in Python: Using with Direct assignmentUs 3 min read How to Create a Python Dictionary from Text File? The task of creating a Python dictionary from a text file involves reading its contents, extracting key-value pairs and storing them in a dictionary. Text files typically use delimiters like ':' or ',' to separate keys and values. By processing each line, splitting at the delimiter and removing extr 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 Like