How to replace characters in string Python?

Last Updated on June 5, 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”.
Using str.

replace ()to replace characters in a string:
Use str.

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



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[2] = “c”
Replace 3rd element
New_string = “”.join(string_list)



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)


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?

Leave a Comment