Python String format_map() Method (original) (raw)
Last Updated : 05 Aug, 2021
Python String_format_map()_ method is an inbuilt function in Python, which is used to return a dictionary key’s value.
Syntax:
string.format_map(z)
Parameters:
Here z is a variable in which the input dictionary is stored and string is the key of the input dictionary. input_dict: Takes a single parameter which is the input dictionary.
Returns:
Returns key’s values of the input dictionary.
Example 1: Python String format_map() method
Python3
a
=
{
'x'
:
'John'
,
'y'
:
'Wick'
}
print
(
"{x}'s last name is {y}"
.format_map(a))
Output:
John's last name is Wick
Example 2:
Python3
a
=
{
'x'
:
"geeksforgeeks"
,
'y'
:
'b'
}
print
(
'{x} {y}'
.format_map(a))
Output:
geeksforgeeks b
Example 3:
Python3
profession
=
{
'name'
:[
'Barry'
,
'Bruce'
],
`` 'profession'
:[
'Engineer'
,
'Doctor'
],
`` 'age'
:[
30
,
31
] }
print
(
'{name[0]} is an {profession[0]} and he'
`` ' is {age[0]} years old.'
.format_map(profession))
print
(
'{name[1]} is an {profession[1]} and he'
`` ' is {age[1]} years old.'
.format_map(profession))
Output:
Barry is an Engineer and he is 30 years old. Bruce is an Doctor and he is 31 years old.
Example 4: Practical Application
The format_map() function can be used in any practical application.
Python3
def
chk_msg(n):
`` a
=
{
'name'
:
"George"
,
'mesg'
:n}
`` print
(
'{name} has {mesg} new messages'
.format_map(a))
chk_msg(
10
)
Output:
George has 10 new messages
Similar Reads
- Python String Methods Python string methods is a collection of in-built Python functions that operates on strings. Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. Python string is a sequence of Unicode characters that is enclosed in quotati 6 min read
- String capitalize() Method in Python The capitalize() method in Python is used to change the first letter of a string to uppercase and make all other letters lowercase. It is especially useful when we want to ensure that text follows title-like capitalization, where only the first letter is capitalized. Let's start with a simple exampl 2 min read
- Python String casefold() Method Python String casefold() method is used to convert string to lowercase. It is similar to the Python lower() string method, but the case removes all the case distinctions present in a string. Python String casefold() Method Syntax Syntax: string.casefold() Parameters: The casefold() method doesn't ta 1 min read
- Python String center() Method center() method in Python is a simple and efficient way to center-align strings within a given width. By default, it uses spaces to fill the extra space but can also be customized to use any character we want. Example: [GFGTABS] Python s1 = "hello" s2 = s1.center(20) print(s2) [/GFGTABS]Ou 2 min read
- Python String count() Method The count() method in Python returns the number of times a specified substring appears in a string. It is commonly used in string analysis to quickly check how often certain characters or words appear. Let's start with a simple example of using count(). [GFGTABS] Python s = "hello world" r 2 min read
- Python - Strings encode() method String encode() method in Python is used to convert a string into bytes using a specified encoding format. This method is beneficial when working with data that needs to be stored or transmitted in a specific encoding format, such as UTF-8, ASCII, or others. Let's start with a simple example to unde 3 min read
- Python String endswith() Method The endswith() method is a tool in Python for checking if a string ends with a particular substring. It can handle simple checks, multiple possible endings and specific ranges within the string. This method helps us make our code cleaner and more efficient, whether we're checking for file extensions 3 min read
- expandtabs() method in Python expandtabs() method in Python is used to replace all tab characters (\t) in a string with spaces. This method allows for customizable spacing, as we can specify the number of spaces for each tab. It is especially useful when formatting text for better readability or alignment. Let's understand with 3 min read
- Python String find() Method find() method in Python returns the index of the first occurrence of a substring within a given string. If the substring is not found, it returns -1. This method is case-sensitive, which means "abc" is treated differently from "ABC". Example: [GFGTABS] Python s = "Welcome to GeekforGeeks!" 2 min read
- Python String format() Method format() method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well-organized output. It replaces the outdated % formatting method, making string interpolation more readable and efficient. E 9 min read