String Replace Doesn't Appear to Be Working

String replace doesn't appear to be working

Strings in Python are immutable. That means that a given string object will never have its value changed after it has been created. This is why an element assignment like some_str[4] = "x" will raise an exception.

For a similar reason, none of the methods provided by the str class can mutate the string. So, the str.replace method does not work the way I think you expect it to. Rather than modifying the string in place, it returns a new string with the requested replacements.

Try:

encrypted_str = encrypted_str.replace(encrypted_str[j], dec_str2[k], 2)

If you're going to be making many such replacements, it may make sense to turn your string into a list of characters, make the modifications one by one, then use str.join to turn the list back into a string again when you're done.

Why replace() doesn't work in my Python function?

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

Why isn't the replace() function working?

strings are immutable. so header_raw_text.replace() does not change the string itself.you have to do reassign the result after replacing.

header_raw_text = header_raw_text.replace("arrow_upward ", "")

python: why does replace not work?

String.replace(substr)

does not happen in place, change it to:

string = string.replace("http://","")

Python String replace doesn't work

Strings are immutable. That means that they cannot be changed. stringT.replace(...) does not change stringT itself; it returns a new string. Change that line to:

stringT = stringT.replace("world", "all")

String.Replace() doesn't work with |

You aren't putting the result back into connectionString

Try

connectionString = Properties.Settings.Default.KDatabaseConnectionString;
connectionString = connectionString.Replace(@"|DataDirectory|", Application.StartupPath);

replace() method not working on Pandas DataFrame

You need to assign back

df = df.replace('white', np.nan)

or pass param inplace=True:

In [50]:
d = {'color' : pd.Series(['white', 'blue', 'orange']),
'second_color': pd.Series(['white', 'black', 'blue']),
'value' : pd.Series([1., 2., 3.])}
df = pd.DataFrame(d)
df.replace('white', np.nan, inplace=True)
df

Out[50]:
color second_color value
0 NaN NaN 1.0
1 blue black 2.0
2 orange blue 3.0

Most pandas ops return a copy and most have param inplace which is usually defaulted to False

Java String.replace/replaceAll not working

Strings are immutable in Java. Make sure you re-assign the return value to the same String variable:

str = str.replaceAll("\\[", "");

For the normal replace method, you don't need to escape the bracket:

str = str.replace("[", "");

string.Replace (or other string modification) not working

Strings are immutable. The result of string.Replace is a new string with the replaced value.

You can either store result in new variable:

var newString = someTestString.Replace(someID.ToString(), sessionID);

or just reassign to original variable if you just want observe "string updated" behavior:

someTestString = someTestString.Replace(someID.ToString(), sessionID);

Note that this applies to all other string functions like Remove, Insert, trim and substring variants - all of them return new string as original string can't be modified.



Related Topics



Leave a reply



Submit