How to use string.replace() in python 3.x

后端 未结 8 1856
旧巷少年郎
旧巷少年郎 2020-11-22 12:29

The string.replace() is deprecated on python 3.x. What is the new way of doing this?

8条回答
  •  执笔经年
    2020-11-22 12:43

    Simple Replace:         .replace(old, new, count) .

    text = "Apples taste Good."
    print(text.replace('Apples', 'Bananas'))          # use .replace() on a variable
    Bananas taste Good.          <---- Output
    
    print("Have a Bad Day!".replace("Bad","Good"))    # Use .replace() on a string
    Have a Good Day!             <----- Output
    
    print("Mom is happy!".replace("Mom","Dad").replace("happy","angry"))  #Use many times
    Dad is angry!                <----- Output
    

提交回复
热议问题