6

Replace all occurrences of a character in String in Python

 2 years ago
source link: https://thispointer.com/replace-all-occurrences-of-a-character-in-string-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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)
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:

Advertisements

vid5ea17e5ab3126602323649.jpg?cbuster=1600267117
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY1MDIlMwE1NSZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDM0MxQmMTM3NUYmMwMlN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwVwNwQ0YWIlY2FyJzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NTAlMwIkNTY3NTIzqWyxPVNyn2yhZG9TUGkurWVlNwI1YmY0NGI4MTx3NSZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZlZXBfYWNyLWFfoC1iY2N1paJyozNypl1iZv1uLWNbYXJuY3Rypv1cov1mqHJcozpgnW4gpHy0nG9hJTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==
The lXst trXin is Xt stXtion.
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)
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.
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)
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.
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

 

 

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.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK