-1

How can I adjust the following function so that if the variable assigned to st1 does not appear at all in string (1 does not appear in string, and the string is *********), the function just returns the string back (*********) but if the string does contain 1, it replaces every 1 with 2 as done below. Thank you for your help.

def replacest1(string,st1,st2):
for i in string:
    if i == st1:
        N_W = string.replace(st1 , st2)
        return N_W
        print(replacest1("***1***1***","1","2"))
Gerhard
  • 22,678
  • 7
  • 27
  • 43
Jason
  • 11
  • 5
  • 3
    I'm not sure you need a function. Given three strings, `foo`, `tok`, and `rep`, simply using `foo.replace(tok, rep)` will return `foo` with all instances of `tok` replaced with `rep`. If `foo` contains *no* instances of `tok`, it will simply return the original string `foo`. Additionally, I think the for loop is unnecessary (and probably causing you the most trouble). – jedwards Nov 10 '17 at 05:53
  • for an assignment, required to replace every instance of st1 with s2. If the string does not contain st1, we just return string. – Jason Nov 10 '17 at 06:00
  • 2
    BTW, `string` is not a good choice for a variable name: it's the name of a standard module. – PM 2Ring Nov 10 '17 at 06:06
  • @PM2Ring: Yes, I was just about saying that. It's quite important to NOT choose identifiers like this. – Regis May Nov 10 '17 at 06:27

1 Answers1

1
def replacest1(string, st1, st2):
    return string.replace(st1, st2)

Test:

replacest1("abcdefdcba", "c", "x")

results in:

"abxdefdxba"

Test:

replacest1("yyyyzzzz", "c", "x")

results in:

"yyyyzzzz"

Or much much much much better:

"abcdefdcba".replace("c", "x")

Please note that the function defined above is a 1:1 correspondence to the function demanded by the question. It is not a good practice to use string as an identifier. I'd strongly recommend not to use such kind of identifier.


A note: As the definition of such a function can be eliminated altogether as string.replace() still exists while it is deprecated the solution above will still contain the identifier string as used in the question. If the function couldn't be eliminated I'd use a different identifier here but as defining an own function isn't necessary at all anyway I let this identifier be as it is for now.


Here is a variant of how to implement that without the usage of string.replace():

def replaceCharacters(inputString, findChar, replaceChar):
    assert isinstance(inputString, str)
    assert isinstance(findChar, str)
    assert len(findChar) == 1
    assert isinstance(replaceChar, str)
    assert len(replaceChar) == 1

    buffer = ""
    for c in inputString:
        buffer += replaceChar if c == findChar else c
    return buffer

A note: I prefer to use asserts in order to detect programming errors. If performance is not a big issue in your use case these asserts don't cause any trouble but prevent accidentally misusing a function like this: If you accidentally pass wrong parameters the asserts will fail and give you feedback immediately.

Regis May
  • 3,070
  • 2
  • 30
  • 51
  • Could you perhaps rename the `string` parameter (perhaps to `source_string`)? I honestly thought you were calling the deprecated [`string.replace`](https://docs.python.org/2/library/string.html#string.replace) function at first. – Arthur Tacca Nov 10 '17 at 07:11
  • Yes, you're right. I am using the deprecated string replace. It still exist though it seems to be no longer documented. I'll update the answer again. – Regis May Nov 10 '17 at 07:42
  • That should be even better now. – Regis May Nov 10 '17 at 07:52