Python replace function [replace once]

前端 未结 6 1536
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 12:40

I need help with a program I\'m making in Python.

Assume I wanted to replace every instance of the word \"steak\" to \"ghost\" (just go wit

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 12:55

    Use the count variable in the string.replace() method. So using your code, you wouold have:

    s="The scary ghost ordered an expensive steak"
    print s
    s=s.replace("steak","ghost", 1)
    s=s.replace("ghost","steak", 1)
    print s
    

    http://docs.python.org/2/library/stdtypes.html

提交回复
热议问题