Replace all occurrences of a character in String in Python

In this article, we will discuss different ways to replace all occurrences of a character from a string in Python.

Table Of Contents

Using replace() function

In Python, the string class provides a function replace(to_be_replaced, replacement) to replace all occurrences of a sub-string in a string. We can also use this function to replace all occurrences of a character from a string. For that, we just need to pass the character to be replaced and the replacement character as arguments to the replace() function.

For example, let’s replace all occurrences of character ‘a’ by ‘X’ in the string,

strValue = "The last train is at station."

# Replace all occurrences of character 'a' in string with 'X`'
strValue = strValue.replace('a', 'X')

print(strValue)

Output:

The lXst trXin is Xt stXtion.

It replaced all the occurrences of character ‘a’ with the character ‘X’ in the given string.

Using Regex

In Python, the regex module provides a function sub(pattern, replacement_str, original_str) to replace the contents of a string based on regex pattern. All the substrings that matches the given regex pattern, will be replaced by replacement string. To replace all the occurrences of a given character from a string, pass the character to be replaced and the replacement character as argument to the regex.sub() function.

For example, let’s replace all occurrences of character ‘a’ by ‘X’ in the string using regex,

import re

strValue = "The last train is at station."

ch = 'a'

# Replace all occurrences of character 'a' with 'X' from string
strValue = re.sub('a', 'X', strValue )

print(strValue)

Output:

The lXst trXin is Xt stXtion.

It replaced all the occurrences of character ‘a’ with the character ‘X’ in the given string.

Using translate() function

In Python, the string class provides a member function translate(translation_table) to change the string contents. It accepts a translation table / mapping as an argument and replaces characters in string based on the mapping in the translation table.

Let’s use this translate() function to replace all the occurrences of a given character from a string. For that create a translation table where character ‘a’ is mapped with ‘X’. Then pass this translation table to the transate() function of string. It will replace all occurrences of character ‘a’ by ‘X’ in the string.

For example:

strValue = "The last train is at station."

# Map the character 'a' with the 'X'
translation_table = str.maketrans('a', 'X')

# Replace all occurrences of character 'a' based
# on translation table / mapping table
strValue = strValue.translate(translation_table)

print(strValue)

Output

The lXst trXin is Xt stXtion.

It replaced all the occurrences of character ‘a’ with the character ‘X’ in the given string.

Summary

We learned about different ways to replace all occurrences of a character from a string in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top