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

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

    FYI, when appending some characters to an arbitrary, position-fixed word inside the string (e.g. changing an adjective to an adverb by adding the suffix -ly), you can put the suffix at the end of the line for readability. To do this, use split() inside replace():

    s="The dog is large small"
    ss=s.replace(s.split()[3],s.split()[3]+'ly')
    ss
    'The dog is largely small'
    
    0 讨论(0)
  • 2020-11-22 12:56

    As in 2.x, use str.replace().

    Example:

    >>> 'Hello world'.replace('world', 'Guido')
    'Hello Guido'
    
    0 讨论(0)
提交回复
热议问题