2

Here is actual code:

def replace_exception_chars(string):
    exception_chars_dict = {'Old': 'New', 'old': 'new'}
    exception_chars_keys = list(exception_chars_dict.keys())
    for exception_char in exception_chars_keys:
        if exception_char in string:
            string.replace(exception_char, exception_chars_dict[exception_char])
    return string

print(replace_exception_chars('Old, not old'))

If I'm trying to run it I'm getting unchanged source string in OUTPUT. Please have a look: enter image description here

Why its happening that way?

UPDATE desired output:

New, not new

Community
  • 1
  • 1
Quanti Monati
  • 769
  • 1
  • 11
  • 35

6 Answers6

3

replace() in not working in-place:

The method replace() returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.

so you miss assignment:

string = string.replace(exception_char, exception_chars_dict[exception_char])
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

The function str.replace() does not alter the string you call it on - it can't, strings are immutable - it returns the new string. You do not save the output to a variable, so it is lost.

You need to swap string.replace(exception_char, exception_chars_dict[exception_char]) for string = string.replace(exception_char, exception_chars_dict[exception_char]).

Ollie
  • 1,641
  • 1
  • 13
  • 31
1

You just forgive to save the value in your loop. The replace method return a string.

def replace_exception_chars(string):
    exception_chars_dict = {'Old': 'New', 'old': 'new'}
    exception_chars_keys = list(exception_chars_dict.keys())
    for exception_char in exception_chars_keys:
        if exception_char in string:
            string = string.replace(
                exception_char, exception_chars_dict[exception_char])
    return string


print(replace_exception_chars('Old, not old'))
# New, not new
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
1

Try this

 string.replace(exception_char, exception_chars_dict[exception_char])

changed to

string = string.replace(exception_char, exception_chars_dict[exception_char])
Larissa
  • 21
  • 1
1

replace is not a in-place method, but instead it returns a new string, so you need to assign the result to a new string.

From the docs: https://docs.python.org/3/library/stdtypes.html#str.replace

str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Also your logic can be simplified a lot like below, if you iterate on key and value together

def replace_exception_chars(string):
    exception_chars_dict = {'Old': 'New', 'old': 'new'}

    #Iterate over key and value together
    for key, value in exception_chars_dict.items():
        #If key is found, replace key with value and assign to new string
        if key in string:
            string = string.replace(key, value)

    return string

print(replace_exception_chars('Old, not old'))

The output will be

New, not new
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

You need to store the values that you are storing. So instead of

string.replace(exception_char, exception_chars_dict[exception_char])

Write

string = string.replace(exception_char, exception_chars_dict[exception_char])

Complete Code

def replace_exception_chars(string):
    exception_chars_dict = {'Old': 'New', 'old': 'new'}
    exception_chars_keys = list(exception_chars_dict.keys())
    for exception_char in exception_chars_keys:
        if exception_char in string:
            string = string.replace(exception_char, exception_chars_dict[exception_char])
    return string

print(replace_exception_chars('Old, not old'))
Shubham Sharma
  • 1,753
  • 15
  • 24