Run
import json # Given string x = 'name=apple,quantity=100,price=50' # Item separator item_sep = ',' # Key and value separator key_value_sep = '=' # Take an empty dictionary to store the result dictionary = dict() # Split items items = x.split(item_sep) # Iterate over items, split them into key and value, and add to dictionary for item in items: key, value = item.split(key_value_sep) dictionary[key] = value print(f"Dictionary\n{dictionary}")
Output