Write a program to replace duplicate word occurence in String


Topic: Write a program to replace duplicate word occurence in String

Solution

str1 = "IISC is the best. IISC has Classes in the evening for professionals. Classes help to learn new things."
repl_dict = {'IISC':'It', 'Classes': 'They'}
str_list = str1.split(' ') 
res = set() 
for idx, ele in enumerate(str_list): 
    if ele in repl_dict: 
        print(str(idx) + '  '+ele)
        if ele in res: 
            str_list[idx] = repl_dict[ele] 
        else: 
            res.add(ele)
res = ' '.join(str_list)
print("Replaced String: " + str(res))



List all Python Programs