Replacing specific words in a string (Python)

后端 未结 3 779
你的背包
你的背包 2020-11-29 10:48

I would like to replace words in a string sentence such as:

What $noun$ is $verb$?

What\'s the regular expression to replace the characters

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 11:13

    You don't need a regular expression for that. I would do

    str = "What $noun$ is $verb$?"
    print str.replace("$noun$", "the heck")
    

    Only use regular expressions when needed. It's generally slower.

提交回复
热议问题