Python String format_map() Method with Examples

String format_map() Method in Python:

The format_map() method is similar to str. format(**mapping), with the difference that str. format(**mapping) creates a new dictionary, whereas str. format map(mapping) does not.

The format map(mapping) method is similar to str.format(**mapping).

The only distinction is that str.format(**mapping) copies the dict, whereas str.format map(mapping) creates a new dictionary during the method call. If you’re working with a dict subclass, this can come in handy.

Syntax:

string. format_map(mapping)

Parameters

format map() only accepts one argument, mapping (dictionary).

Return Value:

format map() formats and returns the given string.

Examples:

Example1:

Input:

Given dictionary = {'a': 7, 'b': -3}
string format = '{a} {b}'

Output:

7 -3

Example2:

Input:

Given dictionary = {'a': 7, 'b': -3, 'c': 10}
string format = '{a} {b} {c}'

Output:

7 -3 10

String format_map() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Pass the given dictionary to the format_map() method to get in the specified format and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict = {'a': 7, 'b': -3}
# Pass the given dictionary to the format_map() method to get in the specified
# format and store it in another variable.
rslt = '{a} {b}'.format_map(gvn_dict)
# Print the above result.
print(rslt)

Output:

7 -3

Similarly, try for the other example

gvn_dict = {'a': 7, 'b': -3, 'c': 10}
rslt = '{a} {b} {c}'.format_map(gvn_dict)
print(rslt)

Output:

7 -3 10

format_map with dict subclass

Approach:

  • Create a class that accepts the dictionary as an argument or constructor.
  • Create a function by passing the key as a parameter to it.
  • Return the key value from the function.
  • In the main function, pass the class with the argument as ‘a’ coordinate to the format_map() method and print it.
  • Similarly, do the same by passing the ‘b’ coordinate.
  • Do the same for the a and b coordinates and print them.
  • The Exit of the Program.

Below is the implementation:

# Create a class that accepts the dictionary as an argument or constructor.
class Coordinatepoints(dict):
   # Create a function by passing the key as a parameter to it.
    def __missing__(self, key):
        # Return the key value from the function.
        return key


# In the main function, pass the class with the argument as 'a' coordinate to the
# format_map() method and print it.
print('({a}, {b})'.format_map(Coordinatepoints(a='3')))
# Similarly, do the same by passing the 'b' coordinate.
print('({a}, {b})'.format_map(Coordinatepoints(b='7')))
# Do the same for the a and b coordinates and print them.
print('({a}, {b})'.format_map(Coordinatepoints(a='3', b='7')))

Output:

(3, b)
(a, 7)
(3, 7)