Home Python How to replace characters in string Python

How to replace characters in string Python

How to replace characters in string Python

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”.


How to replace characters in string Python?

If you want to be able to work with strings in Python, it’s important that they can fit into your program.

You may have noticed when writing programs or doing exercises for school often times there are characters outside of what is allowed within string statements. These extensions will help fix this problem!

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?


Please enter your comment!
Please enter your name here