In this python dictionary program, we will replace words in a string using a dictionary.
Steps to solve the program
- Take a string and a dictionary as input.
- Use for loop to iterate over the keys and values of the dictionary using items().
- If the key in the dictionary is used in the given string, then replace the key with its values in the dictionary using replace().
- Print the output.
string = 'learning python at sqa-tools'
Dict = {'at':'is','sqa-tools':'fun'}
for key, value in Dict.items():
string = string.replace(key, value)
print(string)
Output :
learning python is fun