How to Use String format_map() Method in PythonPython Basics


The format_map() method generates a formatted version of the string based on the mapping dictionary provided.

Note: The format_map() method is similar to str.format(). The difference is that str.format() creates a new dictionary whereas str.format_map() doesn’t.

Example

point = {'x':14, 'y':25}
print('{x} {y}'.format_map(point))

point = {'x':1, 'y':1, 'z': 1}
print('{x} {y} {z}'.format_map(point))

Output:

14 25
1 1 1

Syntax

str.format_map(mapping)

Parameters

The method takes a single parameter which is an input dictionary used for mapping.

Return Value

Returns a formatted version of the string based on mapping dictionary.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us