Python – Case insensitive string replacement

23 Jun, 2022
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 : test_str = “gfg is BeSt”, repl = “good”, subs = “best”
Output : gfg is good
Explanation : BeSt is replaced by “good” ignoring cases.
Input : test_str = “gfg is BeSt”, repl = “better”, subs = “best”
Output : gfg is better
Explanation : BeSt is replaced by “better” ignoring cases.
Method #1 : Using re.IGNORECASE + re.escape() + re.sub()
In this, sub() of regex is used to perform task of replacement and IGNORECASE, ignores the cases and performs case-insensitive replace.
Python3
# Python3 code to demonstrate working of# Case insensitive Replace# Using re.IGNORECASE + re.escape() + re.sub()import re # initializing stringtest_str = "gfg is BeSt" # printing original stringprint("The original string is : " + str(test_str)) # initializing replace stringrepl = "good" # initializing substring to be replacedsubs = "best" # re.IGNORECASE ignoring cases# compilation step to escape the word for all casescompiled = re.compile(re.escape(subs), re.IGNORECASE)res = compiled.sub(repl, test_str)         # printing resultprint("Replaced String : " + str(res))
The original string is : gfg is BeSt
Replaced String : gfg is good
Method #2 : Using sub() + lambda + 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
# Python3 code to demonstrate working of# Case insensitive Replace# Using sub() + lambda + escape()import re # initializing stringtest_str = "gfg is BeSt" # printing original stringprint("The original string is : " + str(test_str)) # initializing replace stringrepl = "good" # initializing substring to be replacedsubs = "best" # regex used for ignoring casesres = re.sub('(?i)'+re.escape(subs), lambda m: repl, test_str)         # printing resultprint("Replaced String : " + str(res))
The original string is : gfg is BeSt
Replaced String : gfg is good
Method #3: Using split(),lower() and replace().You can also use upper() in place of lower().
Python3
# Python3 code to demonstrate working of# Case insensitive Replace # initializing stringtest_str = "gfg is BeSt" # printing original stringprint("The original string is : " + str(test_str)) # initializing replace stringrepl = "good" # initializing substring to be replacedsubs = "best" x = test_str.split()for i in x:    if(i.lower()==subs.lower()):        test_str=test_str.replace(i,repl)# printing resultprint("Replaced String : " + test_str) #contributed by Bhavya Koganti
The original string is : gfg is BeSt
Replaced String : gfg is good
kogantibhavya
Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers