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?


import_contacts

You may also like:

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

  • 18 May

    Escape Curly Brace in f-String in Python

    Table of ContentsUse of the f-string in PythonHow to escape curly brace in f-string in PythonConclusionWas this post helpful? In this tutorial, we will see how to escape curly brace in f-string in Python. Use of the f-string in Python In Python, we can format strings using different methods to get the final result in […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.