Skip to content
Related Articles

Related Articles

Python – Case insensitive string replacement

Improve Article
Save Article
  • Last Updated : 29 Nov, 2022
Improve Article
Save Article

Given a string of words. The task is to write a Python program to replace the given word irrespective of the case with the given string.

Examples:

Input : String = "gfg is BeSt", replace = "good", substring = "best"
Output : gfg is good
Explanation : BeSt is replaced by "good" ignoring cases.

Method 1: Using re.IGNORECASE + re.escape() + re.sub()

In this, sub() of the regex is used to perform the task of replacement, and IGNORECASE ignores the cases and performs case-insensitive replacements.

Python3




import re
 
# initializing string
test_str = "gfg is BeSt"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing replace string
repl = "good"
 
# initializing substring to be replaced
subs = "best"
 
# re.IGNORECASE ignoring cases
# compilation step to escape the word for all cases
compiled = re.compile(re.escape(subs), re.IGNORECASE)
res = compiled.sub(repl, test_str)
         
# printing result
print("Replaced String : " + str(res))


Output

The original string is : gfg is BeSt
Replaced String : gfg is good

Method 2: Using re.sub() + lambda + re.escape() 

Using particular ignore case regex also this problem can be solved. Rest, a lambda function is used to handle escape characters if present in the string. 

Python3




import re
 
# initializing string
test_str = "gfg is BeSt"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing replace string
repl = "good"
 
# initializing substring to be replaced
subs = "best"
 
# regex used for ignoring cases
res = re.sub('(?i)'+re.escape(subs), lambda m: repl, test_str)
         
# printing result
print("Replaced String : " + str(res))


Output

The original string is : gfg is BeSt
Replaced String : gfg is good

Method 3: Using split(), lower() and string replace()

Here, you can also use upper() in place of lower().

Python3




# initializing string
test_str = "gfg is BeSt"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing replace string
repl = "good"
 
# initializing substring to be replaced
subs = "best"
 
x = test_str.split()
for i in x:
    if(i.lower()==subs.lower()):
        test_str=test_str.replace(i,repl)
# printing result
print("Replaced String : " + test_str)
 
#contributed by Bhavya Koganti


Output

The original string is : gfg is BeSt
Replaced String : gfg is good

Method 4: Using a list and join()

Strings are not directly mutable so using lists can be an option. And then get back the string using join on the list.

Python3




# the given input string.
test_str = "gfg is BeSt"
 
# the replace string.
repl = "good"
 
# the string to be replaced.
subs = "best"
 
# create the list.
l = test_str.split(" ")
 
# loop and replace the target string.
for i, w in enumerate(l):
    if w.lower() == subs:
        l[i] = repl
 
# list to string using join.
output = " ".join([i for i in l])
 
print("The original string is :", test_str)
print("Replaced String :", output)


Output

The original string is : gfg is BeSt
Replaced String : gfg is good

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!