In this python function program, we will create a function to create dictionary output from the given string.
Dictionary:
A dictionary consists of a collection of key-value pairs.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function fun.
- Use the def keyword to define the function.
- Pass a parameter i.e. string to the function.
- Create an empty dictionary.
- Create a list of the words in the string using split(” “).
- Use a for loop to iterate over the words in the list.
- Add the first letter and the last letter of the word as the key and the word as the value to the dictionary.
- Return the list.
- Pass the dictionary to the function while calling the function.
def fun(string):
a= {}
l = string.split(" ")
for word in l:
a[word[0]+word[-1]] = word
return a
fun("Python is easy li Learn")
Output :
{'Pn': 'Python', 'is': 'is', 'ey': 'easy', 'li': 'li', 'Ln': 'Learn'}
Related Articles
Python function program to print a list of prime numbers from 1 to 100.
Python function program to get a list of odd numbers from 1 to 100.
Python function program to print and accept login credentials.
Python function program to get the addition with the return statement.
Python function program to create a Fruitshop Management system.
Python function program to check whether the given year is a leap year.