7

Replace all occurrences of a substring in string – Python

 2 years ago
source link: https://thispointer.com/replace-all-occurrences-of-a-substring-in-string-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

Replace all occurrences of a substring in string – Python

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

Suppose we have a string,

"This is the last rain of Season and Jack is here."
"This is the last rain of Season and Jack is here."

After replacing all occurrences of “is” with with the “XX”, the final string should be like,

"ThXX XX the last rain of Season and Jack XX here."
"ThXX XX the last rain of Season and Jack XX here."

There are two different ways to do this. Let’s discuss them one by one,

Using replace() function

The replace(to_be_replaced, replacement) function of string class in Python, provides a direct way to replace all occurrences of a substring with another string in the calling string object.

Advertisements

vid5ea17e5ab3126602323649.jpg?cbuster=1600267117
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY1MDIlMwE3NCZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDM0MxQmMTM3NUYmMwMlN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwVwNwQ1ZDM2NTRuJzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NTAlMwIkNmUkNTtzqWyxPVNyn2yhZG9TUGkurWVlNwI1YmY0NWUkMzRzYlZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZlZXBfYWNyLWFfoC1iY2N1paJyozNypl1iZv1uLXN1YaN0pzyhZl1cov1mqHJcozpgpHy0nG9hJTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==

We can use this to replace all occurrences of “is” with “XX”, by passing them as arguments to the replace() function.

For Example:

strValue = "This is the last rain of Season and Jack is here."
# Replace all occurrences of substring 'is' in string with 'XX'
strValue = strValue.replace('is', 'XX')
print(strValue)
strValue = "This is the last rain of Season and Jack is here."

# Replace all occurrences of substring 'is' in string with 'XX'
strValue = strValue.replace('is', 'XX')

print(strValue)

Output:

ThXX XX the last rain of Season and Jack XX here.
ThXX XX the last rain of Season and Jack XX here.

It replaced all occurrences of substring “is” with “XX” in the given string.

Using Regex

The regex module in Python provides a function sub(pattern, replacement_str, original_str) to replace the substrings in a string based on matching regex pattern. All the substrings that matches the given regex pattern in the original string, will be replaced by the replacement string.

We can use this to replace all occurrences of “is” with “XX. For that we need to pass following arguments to regex.sub() function,

  • “is”: A Regex Pattern that matches all the occurrences of substring “is” in the string.
  • “XX”: The replacement string
  • Original String: The strinh in which we need to replace all the occurrences of substring “is”

For Example:

import re
strValue = "This is the last rain of season and Jack is here."
# Replace all occurrences of substring 'is' in string with 'XX'
strValue = re.sub('is', 'XX', strValue )
print(strValue)
import re

strValue = "This is the last rain of season and Jack is here."

# Replace all occurrences of substring 'is' in string with 'XX'
strValue = re.sub('is', 'XX', strValue )

print(strValue)

Output:

ThXX XX the last rain of Season and Jack XX here.
ThXX XX the last rain of Season and Jack XX here.

Strings in Python are immutable. We can modify its contents but we can create a copy of the string with the modified contents.

The regex.sub() function returned a copy of original string with the modified contents. We can assign that back to the original variable. It will give an effect that all occurrences of substring “is” with “XX” in the given string.

Summary:

We learned about two different ways to replace all occurrences of a substring in 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