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.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.