Replace a Character in a String Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A string is an immutable data type that is used to store the sequence of Unicode characters. For replacing a string in Python, we have various ways. One of the most widely used ways is using the Python built-in method string.replace().

The .replace() method replaces the old character with a new character. We can also use loops to replace a character in a string in Python. The string slicing method can also be used to replace a string in python. The regex module is a built-in Python module that can be used to replace a string Python.

How to Replace a Character in a String in Python?

Before getting into various approaches of how to replace a character in a string Python, let us briefly discuss strings in python.

A String is an immutable data type that is used to store the sequence of Unicode characters. In Python, there is no character data type so even a single character is treated as a string having a length of 1. Strings are one of the most widely used data types of any programming language. A string in Python can be easily created using quotes (either single quotes or double quotes).

Example: string = Scaler Topics or Example: string = Scaler Topics

Now, let us take an example to understand what replacing a character in a string in Python means:

Given Original string = bad. We need to replace the character d with the character t. So, the new string becomes bat.

There are multiple ways to achieve this in Python:

  • Using the string replace() method
  • Using Loops
  • Using string slicing
  • Using a list
  • Using regular expressions (regex)

We will go over each of these methods in the subsequent section in detail.

Method 1 : Python String Replace

We can use the Python built-in method string.replace() to replace a string in python.

Let us briefly discuss the replace() method before the implementation.

The string.replace() is a built-in method associated with strings in Python. It replaces all the occurrences of a particular substring from the method invoking string and returns a copy of the replaced string.

The syntax of replace() method is very simple:

Here, old_string is the string that we want to replace. new_string is the string that would replace the old string. The counter is an optional parameter that tells the Python interpreter the number of times the old_string has to be replaced by the new_string.

As we have discussed above, the string.replace() method return a copy of the changed string. It does not change the original string.

If we have not specified the number of times the replacement has to be performed (counter is not specified), the n all the occurrence of the old_sting is replaced with the new_string. Learn more about replace() method here.

Let us see the implementation.

Output:

In the code above, we have replaced the character d with the character t. After the replacement, the new string is stored in the replaced_string and we printed it.

Method 2: Replace a character in a string using a loop in python

We can also use loops to replace a character in a string in Python.

A loop is a conditional iterative statement that checks certain condition(s) before repeatedly executing the statement present inside the loop body. We have for loop and while loop in Python.

The idea is very simple, we will check each character of the string, if the current character of the string is to be replaced, we will replace it and move to the next character. this process of checking and replacing will continue till all the characters of the original string are traversed.

Let us see the implementation for a better understanding.

Output:

In the code above, we have replaced the character d with the character t. After the replacement, the new string is stored in the replaced_string and we printed it.

Method 3: Using Slicing Method

String slicing is the operation using which we can obtain a substring of a string by using the indices of the string. The slicing method is used to access various parts or sequences of a string.

The syntax for slicing in python is:

Or

Here, starting_index is the index where the slicing is to begin and stop_index is the index where the slicing is to be stopped. We can also specify the number of increments between each index for slicing using the optional parameter steps.

Using the slicing method, we can choose the character that has to be replaced by slicing the original string. After slicing, we can easily replace the required character with the new character.

Let us see the implementation for a better understanding.

Output:

bat

Method 4: Replace Character At a Given Position In a String Using List

A list is used to store one or more objects or data elements. Lists are used in python and java mostly. We can say that lists are similar to arrays. A list is a mutable data structure.

We can convert the string into a list and then replace the old character at the particular index with a new character. After the replacement, we can convert the new list into the string using the list.join() method to form a result string again.

Let us see the implementation for a better understanding.

Output:

In the code above, we have replaced the character d with the character t. After the replacement, the new string is printed by converting the list into the string using the join() method.

Method 5: Python Regex Module

The regex module is used to handle the text data and perform operations such as finding the substring, replacing the string, and operations like that. The regex module is a built-in Python module that can be used to replace a string Python. (Learn more about regex in Python here)

We can use re.sub() method for the same.

Syntax of re.sub() method is:

The re.sub() method of this module, takes three parameters namely - the string or the pattern that has to be replaced (old_pattern), the new string or the character that will replace the old pattern (new_pattern), and the original string on which the operation has to be performed (original_pattern).

Let us see the implementation for a better understanding.

Output:

In the code above, we have replaced the character d with the character t. After the replacement, the new string is stored in the replaced_string and we printed it.

Conclusion

  • A string is an immutable data type that is used to store the sequence of Unicode characters.
  • We can use the Python built-in method string.replace() to replace a string in python.
  • We can also use loops to replace a character in a string in Python.
  • String slicing method can also be used to replace a string in python. We can choose the character that has to be replaced by slicing the original string. After slicing, we can easily replace the required character with the new character.
  • We can convert the string into a list and then replace the old character at the particular index with a new character.
  • We can also use the regex module which is a built-in Python module that can be used to replace a string Python.
  • The string.replace() is one of the best ways to replace a string in python.

Read More: