Why replace() doesn't work in my Python function?
Question:
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:
Why its happening that way?
UPDATE
desired output:
New, not new
Answers:
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])
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])
.
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
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'))
Try this
string.replace(exception_char, exception_chars_dict[exception_char])
changed to
string = string.replace(exception_char, exception_chars_dict[exception_char])
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
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:
Why its happening that way?
UPDATE
desired output:
New, not new
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])
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])
.
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
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'))
Try this
string.replace(exception_char, exception_chars_dict[exception_char])
changed to
string = string.replace(exception_char, exception_chars_dict[exception_char])
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