You are currently viewing String Replacement in Python

In this article, we will see how to replace sub-string in a string in Python programming language.

Advertisements

It can be possible to replace string using replace() function and re.sub() method available in ‘re’ package.

1. Quick Examples of String replacement

Following are quick examples of string.find() method.


import re

# Consider the string 
string1="welcome to sparkby. sparkby is a website that hold python tutorials and welcome to python"

# Replace "python" with "java" using replace()
print(string1.replace("python","java"))

# Replace first occurrence of "python" with "java" using replace() with count parameter.
print(string1.replace("python","java",1))

# Replace all occurrences of "python" with "java" and display the string using re.sub().
print(re.sub("python", "java", string1))

# Replace first occurrence of  "sparkby" with "SPARKBY" and display the string using re.sub() with count parameter.
print(re.sub("sparkby", "SPARKBY", string1,1))

# Replace all occurrences of "python" with "java" and display the string using re.sub() with flags parameter.
print(re.sub("python", "java", string1,flags=re.IGNORECASE))

2. Python String replace()

replace() method is used to replace the old substring with new string. If there are multiple occurrences of same substrings, it can be possible to replace only some/all substrings using the count parameter. If this parameter is not specified, all the substrings will be replaced. It is an optional parameter.

2.1 Syntax of String replace()

The following is the syntax of the String replace() function.


mystring.replace(old_substring, new_substring, count)

2.2 Parameters of String replace()

It takes three parameters.

  1. old_substring is the substring that exists in mystring.
  2. new_substring replaces the old_substring.
  3. count takes an integer that specifies number of occurrences to be replaced.

2.3 Return

It returns the string with replaced substring/s.

3. Python String replace() Examples

Let’s see the examples to understand the concept better.

3.1 – Without count parameter

Let’s replace all occurrences of substrings.


# Consider the string
string1="welcome to sparkby. sparkby is a website that hold python tutorials and welcome to python"

print("Original: ",string1)

# Replace "python" with "java" and display the string.
print(string1.replace("python","java"))

# Replace "sparkby" with "SPARKBY" and display the string.
print(string1.replace("sparkby","SPARKBY"))

# Replace "welcome" with "Hello" and display the string.
print(string1.replace("welcome","Hello"))

# Original:  welcome to sparkby. sparkby is a website that hold python tutorials and welcome to python
# welcome to sparkby. sparkby is a website that hold java tutorials and welcome to java
# welcome to SPARKBY. SPARKBY is a website that hold python tutorials and welcome to python
# Hello to sparkby. sparkby is a website that hold python tutorials and Hello to python

Explanation:

  1. In the second output, we are displaying the string by replacing “python” with “java”. There are 2 “python” substrings in the string. Both of them were replaced by “java”.
  2. In the third output, we are displaying the string by replacing “sparkby” with “SPARKBY”. There are 2 “sparkby” substrings in the string. Both of them were replaced by “SPARKBY”.
  3. In the last output, we are displaying the string by replacing “welcome” with “Hello”. There are 2 “welcome” substrings in the string. Both of them were replaced by “Hello”.

3.2 – With count parameter

Let’s specify the count parameter and replace some occurrence of substrings.


# Consider the string
string1="welcome to sparkby tutorial. sparkby is a website that hold python tutorial and welcome to python tutorial"

print("Original: ",string1)

# Replace first occurrence of "python" with "java" and display the string.
print(string1.replace("python","java",1))

# Replace first occurrence of  "sparkby" with "SPARKBY" and display the string.
print(string1.replace("sparkby","SPARKBY",1))

# Replace first two occurrences of "tutorial" with "topic" and display the string.
print(string1.replace("tutorial","topic",2))

# Output:
# Original:  welcome to sparkby tutorial. sparkby is a website that hold python tutorial and welcome to python tutorial
# welcome to sparkby tutorial. sparkby is a website that hold java tutorial and welcome to python tutorial
# welcome to SPARKBY tutorial. sparkby is a website that hold python tutorial and welcome to python tutorial
# welcome to sparkby topic. sparkby is a website that hold python topic and welcome to python tutorial

Explanation:

In the second output, we are displaying the string by replacing “python” with “java”. There are 2 “python” substrings in the string. Only first occurrence is replaced by “java” since we specified count as 1.
In the third output, we are displaying the string by replacing “sparkby” with “SPARKBY”. There are 2 “sparkby” substrings in the string. Only first occurrence is replaced by “SPARKBY” since we specified count as 1.
In the last output, we are displaying the string by replacing “tutorial” with “topic”. There are 3 “tutorial” substrings in the string. Only first two were replaced by “topic” since we specified count as 2.

4. Python String re.sub()

re.sub() method is used to replace the old substring with new string using pattern. It is available in the re module, so it is important to import this package. It will take five parameters.

4.1 Syntax of String replace()

The following is the syntax of the String replace() function.


re.sub(pattern,new_string, mystring, count,flags)

4.2 Parameters of String re.sub()

It takes five parameters.

  1. pattern specifies the substring that has to be replaced in mystring.
  2. new_substring replaces the old substring (pattern).
  3. mystring is the input string.
  4. count takes an integer that specifies number of occurrences to be replaced. It is optional.
  5. flags is an optional parameter which helps to consider the case-sensitivity while replacing. Using this parameter, we can ignore case-sensitivity,

4.3 Return

It returns the string with replaced substring/s.

5. Python String re.sub() Examples

Let’s see the examples to understand the concept better.

5.1 – Without flags parameter

Let’s replace some substrings


import re

# Consider the string
string1="welcome to sparkby tutorial. Sparkby is a website that hold Python tutorial and welcome to python tutorial"

print("Original: ",string1) 

# Replace all occurrences of "python" with "java" and display the string.
print(re.sub("python", "java", string1))

# Replace first occurrence of  "sparkby" with "SPARKBY" and display the string.
print(re.sub("sparkby", "SPARKBY", string1,1))

# Output:
# Original:  welcome to sparkby tutorial. Sparkby is a website that hold Python tutorial and welcome to python tutorial
# welcome to sparkby tutorial. Sparkby is a website that hold Python tutorial and welcome to java tutorial
# welcome to SPARKBY tutorial. Sparkby is a website that hold Python tutorial and welcome to python tutorial

Explanation:

  1. In second output, we are replacing “python” with “java”. There is only one “python” so it will be replaced by “java”. We can observe the case-sensitivity. “Python” is not replaced with “java”.
  2. In the last output, we are replacing first occurrence of “sparkby” with “SPARKBY”. There is only one “sparkby” so it will be replaced by “SPARKBY”. We can observe the case-sensitivity. “Sparkby” is not replaced with “SPARKBY”.

5.2 – With flags parameter

Let’s replace some substrings without considering case-sensitivity.


import re

# Consider the string
string1="welcome to sparkby tutorial. Sparkby is a website that hold Python tutorial and welcome to python tutorial"

print("Original: ",string1) 

# Replace all occurrences of "python" with "java" and display the string.
print(re.sub("python", "java", string1,flags=re.IGNORECASE))

# Replace all occurrences of  "sparkby" with "SPARKBY" and display the string.
print(re.sub("sparkby", "SPARKBY", string1,flags=re.IGNORECASE))

# Output:
# Original:  welcome to sparkby tutorial. Sparkby is a website that hold Python tutorial and welcome to python tutorial
# welcome to sparkby tutorial. Sparkby is a website that hold java tutorial and welcome to java tutorial
# welcome to SPARKBY tutorial. SPARKBY is a website that hold Python tutorial and welcome to python tutorial

Explanation:

  1. In second output, we are replacing “python” with “java”. We are ignoring the case-sensitivity. Hence “java” replaces “python” and “Python”.
  2. In the last output, we are replacing first occurrence of “sparkby” with “SPARKBY”. We are ignoring the case-sensitivity. Hence “java” replaces “sparkby” and “Sparkby”.

6. Differences between replace() and re.sub()

replace()re.sub()
We can use this method directly.We have to import re module to use this method.
Case-sensitivity can’t be controlled using this method.Case-sensitivity can be controlled using this method.

7. Conclusion

In this article, we seen two ways to replace the substrings in a string in Python. Based on your requirement, you can use any of the above two discussed methods. Make sure that you need to import “re” module while using sub() method.