How to Replace Word in String in Python

In this tutorial, we will to how to replace word in string in Python.

Strings represent a sequence of characters. In Python, they are immutable, which means that we cannot edit or modify strings. Whenever we perform any operation of modifying a string, we are creating a new copy.

We will discuss how to replace word in string in Python in the methods below.

Using the replace() Function to replace word in string in Python

The string class in Python has many functionalities available to work with such objects. The replace() function belongs to this class and can be used to replace a given substring in a string.

We can use this function to replace word in string in Python.

For example,

Output:

python 2 blog

In the above example, we create a string s1 and replace a substring from this string using the replace() function. The new string is displayed.

By default, all the possible occurrences of the substring are replaced using this function. However, it accepts a count parameter in which we can specify how many occurrences we want to replace.

Using the re.Sub() Function to replace word in string in Python

Regular expressions (regex) are used for reading and detecting substrings from a string based on a regex pattern. In Python, we can use the re module to work with regex.

The re.sub() function reads a string and substitutes the substring that matches a given pattern with another substring.

See the code below.

Output:

python 2 blog

In the above example, we use the re.sub() function to replace word in string in Python. We can directly specify the substring to be replaced in the function rather than creating some elaborate pattern.

We can use the flags parameter and specify its value as re.IGNORECASE to match the substring regardless of the case.

Another way to use this function is by using the re.compile() function. This function can be used to compile a regex pattern first and be used with functions later on.

We can use this to replace word in string in Python.

For example,

Output:

python 2 blog

Using the re.subn() function to replace word in string in Python

The re.subn() function works similarly to the previous function to replace word in string in Python. The difference lies in the format of the result.

This function returns a tuple containing the new string and the number of matches found. It is used exactly like the re.sub() function.

See the code below.

Output:

(‘python 2 blog’, 1)

Conclusion

In this article, we discussed several ways to replace word in string in Python. The first method uses the replace() function from the string class to replace word in string in Python. We use the sub() function from the re library in the second method that works with regular expressions. The re.subn() function was also discussed. It works the same as the previous method but returns output in a different structure.

That’s all about how to replace word in String in Python.

Was this post helpful?

Leave a Reply

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