Ways to Replace of character in a Python string

In this post, we are going to understand Ways to Replace of character in a Python string. It can be one character or multiple characters and the occurrence of a character.

What is str.replace() in Python


The string built-in function replace() returns a copy of the string in which all occurrence of substring replace with another new substring. All occurrences can be changed as per our requirements.

Syntax

string.replace(old,new,count)
  • old: The old substring in the original string we have to change.
  • New: The new substring by which we have to replace the old substring.
  • count: It is an optional parameter.No of occurrence of the old substring replace with a new subString.

Note: If count is not passed, then all the occurrence of the old substring replace with a new substring.

1. Replace all occurrence of characters in a String

In this code example, we are replacing all occurrence of characters in a given string using the Python string replace() method.

Example : Replace of character in a string Python



orginal_str = 'HoH OHH you.'
substring = 'R'

replace_Str = orginal_str.replace('H','R')
print('replaced string is =\n',replace_Str)

Output will be string after replacement of character in a string Python.

replaced string is =
 RoR ORR you.

As we can see we did not specify the three parameter(count).All the occurrence of character ‘H’ is replaced with R.

2. Replace n(count) number of character in a string


In this code example, we will pass the third parameter count in replace() method to specify how many occurrences of character we have to replace in a string with a new substring.

Code Example : Program to Replace of character in a string Python



#original string

orginal_str = 'hoh Ohh you haha.'

 #new substring

substring = 'R'

replace_Str = orginal_str.replace('h','R',4)

print('The replace original string is =\n',replace_Str)

Output

The replace original string is =
 RoR ORR you haha.

We have specified the count of occurrence of character ‘h’ is we want to replace with character ‘R’.So it has only replaced only 4 characters not all.

3. Replace Multiple characters or string


While dealing with string in data manipulation replace of a single character is not enough, we have to often replace multiple characters or strings. In this code example, we will understand how we can achieve this. These are the step to accomplish this

  • we have a list (list_substring) of characters we want to replace with the new subString “tr”.
  • We are looping over the list of characters we have to replace and checking with if statement, element exists in orginal_str.
  • If it exists in the original string we are replacing so on for all elements of the list (list_substring) of characters.

Code Example

# Program to Replace of character in a string Python

#original string
orginal_str = 'hoh Ohh you haha.'

 #new substring

list_substring = ['h','a','o']

for char in list_substring:
  if char in orginal_str:
   orginal_str = orginal_str.replace(char,"tr")
print('The replace original string is =\n',orginal_str)

Output

The replace original string is =
 trtrtr Otrtr ytru trtrtrtr.

4. Replace the characters user Input string

In this code example, we are taking string as input by user as well character we want to replace.

Code Example

# Program to Replace of character in a string Python

orginal_str= input("Enter the string  to replace : ")

old_char = input("Enter chacter you want to replace : ")

new_char = input(" Enter chacter you want to replace with  : ")

orginal_str = orginal_str.replace(old_char, new_char)

print('The replaced original string is =\n',orginal_str)

Output

Enter the string  to replace : ohh yhhh hahah
Enter chacter you want to replace : h
 Enter chacter you want to replace with  : r
The replaced original string is =
 orr yrrr rarar

Conclusion

We have understood different ways to Replace a character in a string Python or list of characters and user input string using replace() method.