Last Updated on July 14, 2021 by admin
If you are still working on python so, most of the time you have to replace characters in string using Python.
Replacing every event of a character in a string makes another string where every occurrence is supplanted with another character.
For instance, supplanting “a” with “b” in “aba” results in “bbb”.
replace() to replace characters in a string:
replace(old, new) to replace all occurrences of a character old with the desired character new.
This will replace multiple occurrences of the character anywhere in the string.
a_string = “aba” a_string = a_string.replace(“a”, “b”) Replace in a_string print(a_string)
Using list() to convert characters in string:
Utilize the list ordering linguistic structure list[i] to allocate the ideal character to list I.
Call str. join(iterable) with the unfilled string “” as strand the list as iterable to join the characters into another string.
This is viable for replacing a particular file.
old_string = “aba” stringiest = list(old_string) string_list = “c” replace 3rd element new_string = “”.join(string_list) print(new_string)
Regex in python.
Using regex in python to replace characters in strings:
Python gives a regex module (re), and in this module, it gives a capacity sub() to replace the content of a string dependent on designs.
We can utilize this re. sub() capacity to substitute/supplant all events of a character in the string.Sub() function used the first argument as a pattern and replaced all the matches of that pattern with the given replacement string, i.e., ‘X’.
So, it replaced all the occurrences of character ‘s’ with the character ‘X’.
As strings are immutable in python i.e., we cannot change their contents.
Therefore sub() function of the regex module returns a copy of the string with the replaced content.
Using loop in python to replace characters in the string:
Introduce an empty string and afterward repeat the overall characters of the first string.
During the cycle, add each character to the new string.
Yet, for the characters that need replacement, utilize the substitution character all things being equal.
Replace the first two occurrences of a character in string in python:
Instead of replacing all occurrences of a character in a string, we can replace only the first few occurrences of a character in a string bypassing the counter-argument in the replace() function.
org_string = “This is a sample string” # Replace first two occurrences of a character in string new_string = org_string.replace(‘s’, ‘X’, 2) print(new_string)
ThiX iX a sample string
Here we passed the character to be supplanted ‘s’ as the primary contention and character ‘X’ as the second argument.
At that point, we passed the third argument as 2.
The third argument is discretionary and it tells the supplant() work that the number of events of a given sub-string should be supplanted.
Then replace() method returned a copy of the original string by replacing only the first two occurrences of ‘s’ with the ‘X’.
Read More: How to convert python to java script?