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

后端 未结 8 1882
旧巷少年郎
旧巷少年郎 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'
    

提交回复
热议问题