Python String Replace method with examples

Read Time:3 Minute, 42 Second

Table of Contents

The replace() method is responsible for substituting one phrase for another. If nothing else is supplied, all occurrences of the specified phrase are substituted. replace() is a Python built-in function that returns a string copy with all occurrences of a substring replaced with another substring.

You can use the Python method .replace() to replace all instances of a specified character with a new one. You can even specify a new line of text to replace an entire text string. The .replace() method returns a string copy. The old substring remains unchanged, but a new copy is made with the new text replacing all the old content.

Python String Replace method

The syntax is as follows:

Values of Parameters

  • oldvalue is needed. It refers to the phrase to look for in the given string.
  • newvalue is necessary. It is the string that will use to replace the old value.
  • count is Optional. It refers to a number indicating how many times you should replace the old value. All occurrences are the default.

Return Value of replace()

The replace() method copies the string and replaces the previous substring with the new substring. The original string has not been altered. If the old substring cannot be retrieved, the copy of the original string is returned.

Example: Python .replace() Method

Each word containing the character ‘P’ is replaced with the character ‘K’ in the example above. There was a single instance of the letter P in that example. It was discovered in the word Python.

SUGGESTED READ

What if you merely wanted to add an ‘a’ instead of a ‘p’ to two terms, such as Python and coding?

How to Replace a Single Character in a Limited Number of Places

You’d use the count argument and set it to two to modify only two instances of a single character:

Set the count option to one if you just want to modify the first instance of a single character:

How to Replace All String Instances

The procedure for changing several characters is similar.

The term ‘test’ is changed with the word CAT in the previous example.

SUGGESTED READ

How to Replace Only a Limited Number of String Instances

You would use the count option and set it to one if you simply wanted to transform the first instance of test to CAT.

How to Substitute Case-Insensitive Substrings in Python

Consider the following scenario.

What we actually wanted to accomplish in this scenario was to replace all instances of Python with JAVA. However, there was a lowercase ‘p’ in the term python, which we would like to fix as well.

It stayed the same and didn’t change to JAVA because the first letter was lowercase, not uppercase, as requested with Python. Because the .replace() method is case sensitive, it conducts case-sensitive substring substitutions. You’d have to do something different to accomplish a case-insensitive substring substitution. The re.sub() method and the re.IGNORECASE options would be required.

To use re.sub(), you must first:

SUGGESTED READ

  • Import re to use the re module.
  • Make a pattern of regular expressions.
  • Mention what you’d like to replace the pattern with.
  • Mention the string on which we will perform this action.
  • Optionally, specify the count option to fine-tune the replacement and the maximum number of replacements you want to do.
  • The re.IGNORECASE setting instructs the regular expression to match without regard for the case.

As a result, the syntax is as follows:

Using the above example as an example:

Example: replace the word “Apple”

Example: Replacing all occurrences of the word “Google”:

Example: Replacing the two initial occurrences of the word “Google”:

Example: Using the method replace()

Example: Te usage of replace() method

Example: a program for demonstrating the use of replace() method

Conclusion

With the help of examples, we have learned about the Python replace() method in this article. It replaces each old character/text occurrence in the string with the new character/text using the replace() method. The replace() function in Python returns a copy of the string with all occurrences of old replaced with new, with the number of replacements limited to max.

Source: https://www.codeunderscored.com/python-string-replace-method-with-examples/

CyberSEO Pro - OpenAI GPT-3 autoblogging and content curation plugin for WordPress