-3
str1 = "series of sentences that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few **sentences** should be organized into paragraphs."

REPLACE_STRING = {"series":"web" , "sentence":"long paragraph"}

Output:

web of long paragraph that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few **long paragraph** should be organized into paragraphs.

Basically need to replace all values in a str with the key present in the dictionary in one go.

End user
  • 77
  • 3

2 Answers2

0

Try this.

for key, value in REPLACE_STRING.items():
    str1 = str1.replace(key, value)

print(str1)
Underoos
  • 4,708
  • 8
  • 42
  • 85
0

In one go:

str1 = str1.replace('series', 'web').replace('sentence', 'long paragraph')

Using a loop:

for to_replace, replace_with in REPLACE_STRING.items():
    str1 = str1.replace(to_replace, replace_with)
Eeshaan
  • 1,557
  • 1
  • 10
  • 22