How can I use a dictionary to do multiple search-and-replace operations? [duplicate]

address = "123 north anywhere street"

for word, initial in {"NORTH":"N", "SOUTH":"S" }.items():
    address = address.replace(word.lower(), initial)
print address

nice and concise and readable too.

Leave a Comment