Replace Characters Not Working in Python

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.

replace characters not working in python

string.replace() returns the string with the replaced values. It doesn't modify the original so do something like this:

link['href'] = link['href'].replace("..", "")

python: why does replace not work?

String.replace(substr)

does not happen in place, change it to:

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

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 is the .replace() method not working?

Save s2.replace() somewhere and print it.

for i in s2:
if i in checkFor:
print(i)
s2 = s2.replace(i, '')

print(s2)

python multiple characters replace in string not working with pipe

You can do that using re.sub(). Since $ has a special meaning in re, we need to escape it by appending a \ in front of it.

  • (AUS|US|HK|MK)\$ - Finds a match that has either of AUS, US, HK or MK that is followed by a $.
  • re.sub(r'(AUS|US|HK|MK)\$',r'', s) - Replaces the matched string with a '' of string s.
import re

s = "US$0.18 AUS$45 HK$96"
x = re.sub(r'(AUS|US|HK|MK)\$',r'', s)
print(x)
0.18 45 96

Python string.replace() not replacing characters

That's because filename and foldername get thrown away with each iteration of the loop. The .replace() method returns a string, but you're not saving the result anywhere.

You should use:

filename = line[2]
foldername = line[5]

for letter in bad_characters:
filename = filename.replace(letter, "_")
foldername = foldername.replace(letter, "_")

But I would do it using regex. It's cleaner and (likely) faster:

p = re.compile('[/:()<>|?*]|(\\\)')
filename = p.sub('_', line[2])
folder = p.sub('_', line[5])

Python - replace not working on special character in a list

Look at the construction element:

[i for i in lista if ...

You check the isalpha suitability of the string, but what you keep for your list is the original value of i !

Instead, you want to keep the altered value of i; just do any existing replacements, and forget the isalpha check entirely.

[i.replace("\{", "-").replace("\xad", "-").replace("\xa", "-") for i in lista]

Note that, if there are no occurrences of the special chars in the string, you simply get the original in your final list.



Related Topics



Leave a reply



Submit