Program to replace words in a string using a dictionary.

In this python dictionary program, we will replace words in a string using a dictionary.

Steps to solve the program
  1. Take a string and a dictionary as input.
  2. Use for loop to iterate over the keys and values of the dictionary using items().
  3. If the key in the dictionary is used in the given string, then replace the key with its values in the dictionary using replace().
  4. 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
				
			

group the same items into a dictionary values.

remove a word from the string if it is a key in a dictionary.

Leave a Comment