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

后端 未结 8 1900
旧巷少年郎
旧巷少年郎 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:50

    The replace() method in python 3 is used simply by:

    a = "This is the island of istanbul"
    print (a.replace("is" , "was" , 3))
    
    #3 is the maximum replacement that can be done in the string#
    
    >>> Thwas was the wasland of istanbul
    
    # Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached
    

提交回复
热议问题