In this article, we will discuss different ways to replace a character in string in python.
Overview:
- Replace all occurrences of a character in string using replace()
- Replace the first two occurrences of a character in string
- Replace a character in a string using regex
- Replace a character in a string using for loop
Replace all occurrences of a character in string in python using replace()
In python, the String class (Str) provides a method replace() to replace the sub-strings in a string. We can use that to replace all the occurrences of a character in a string with another character. For example,
org_string = "This is a sample string" # Replace all occurrences of a character in string in python new_string = org_string.replace('s', 'X') print(new_string)
Output:
ThiX iX a Xample Xtring
Here we passed the character to be replaced ‘s’ as the first argument and character ‘X’ as the second argument. Then replace() method returned a copy of the original string by replacing all the occurrences of character ‘s’ with the ‘X’.
As strings are immutable in Python, i.e., we cannot change its contents. Therefore replace() function returns a copy of the string with the replaced content.
Know more about Str.replace() method and check out more examples.
Frequently Asked:
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 by passing the count argument in the replace() function i.e.
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)
Output:
ThiX iX a sample string
Here we passed the character to be replaced ‘s’ as the first argument and character ‘X’ as the second argument. Then we passed the third argument as 2. The third argument is optional and it tells the replace() function that how many occurrences of given sub-string need to be replaced.
Then replace() method returned a copy of the original string by replacing only first two occurrences of ‘s’ with the ‘X’.
As strings are immutable in Python, i.e., we cannot change its contents. Therefore replace() function returns a copy of the string with the replaced content.
Replace a character in a string using regex in python
Python provides a regex module (re), and in this module, it provides a function sub() to replace the contents of a string based on patterns. We can use this re.sub() function to substitute/replace all occurrences of a character in the string,
import re # Replace a character in string using regex in python new_string = re.sub('s', 'X', org_string) print(new_string)
Output:
ThiX iX a Xample Xtring
Here we passed the character to be replaced ‘s’ as the first argument and character ‘X’ as the second argument in the sub() function. Then we passed the third argument as the original 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 its contents. Therefore sub() function of the regex module returns a copy of the string with the replaced content.
Replace a character in a string using for loop in python
Initialize an empty string and then iterate over all characters of the original string. During iteration, add each character to the new string. But for the characters that needs replacement, use the replacement character instead. For example,
to_replace = 's' replaced = 'X' # Replace a character in string using for loop new_string = '' for elem in org_string: if elem == to_replace: new_string += replaced else: new_string += elem print(new_string)
Output:
ThiX iX a Xample Xtring
It replaced all the occurrences of character ‘s’ with the ‘X’.
As strings are immutable in Python, i.e., we cannot change its contents. Therefore, we created a new copy of the string with the replaced content.
Summary
We can replace a character in a string with another character in python by using the replace() function or sub() function or a for loop.