By using str.format() function
The str.format() function is used to format a specified string using a dictionary.
Syntax: str .format(value)
Parameters: This function accepts a parameter which is illustrated below:
- value: This is the specified value that can be an integer, floating-point numeric constant, string, characters, or even variables.
Return values: This function returns a formatted string using a dictionary.
Example: Formatting a string using a dictionary
Python3
# Python program to illustrate the # formatting of a string using a dictionary # Initializing a value value = { "Company" : "w3wiki" , "Department" : "Computer Science" } # Calling the .format() function # over the above given value print ( "{Company} is a {Department} Portal." . format ( * * value)) |
Output:
w3wiki is a Computer Science Portal.
How to format a string using a dictionary in Python
In this article, we will discuss how to format a string using a dictionary in Python.