Python – Case insensitive string replacement
Last Updated :
11 Mar, 2024
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.
Case insensitive string replacement using re.IGNORECASE + re.escape() + re.sub()
In this, span>sub()span> of the regex is used to perform the task of replacement, and IGNORECASE ignores the cases and performs case-insensitive replacements.
Python3
import re
test_str = "gfg is BeSt"
print ( "The original string is : " + str (test_str))
repl = "good"
subs = "best"
compiled = re. compile (re.escape(subs), re.IGNORECASE)
res = compiled.sub(repl, test_str)
print ( "Replaced String : " + str (res))
|
Output
The original string is : gfg is BeSt
Replaced String : gfg is good
Case insensitive string replacement 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
test_str = "gfg is BeSt"
print ( "The original string is : " + str (test_str))
repl = "good"
subs = "best"
res = re.sub( '(?i)' + re.escape(subs), lambda m: repl, test_str)
print ( "Replaced String : " + str (res))
|
Output
The original string is : gfg is BeSt
Replaced String : gfg is good
Case insensitive string replacement using split(), lower() and string replace()
Here, you can also use upper() in place of lower().
Python3
test_str = "gfg is BeSt"
print ( "The original string is : " + str (test_str))
repl = "good"
subs = "best"
x = test_str.split()
for i in x:
if (i.lower() = = subs.lower()):
test_str = test_str.replace(i,repl)
print ( "Replaced String : " + test_str)
|
Output
The original string is : gfg is BeSt
Replaced String : gfg is good
Time Complexity: O(n), where n is length of test_str.
Auxiliary Space: O(1)
Case insensitive string replacement 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
test_str = "gfg is BeSt"
repl = "good"
subs = "best"
l = test_str.split( " " )
for i, w in enumerate (l):
if w.lower() = = subs:
l[i] = repl
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
Time Complexity: O(n), where n is
Auxiliary Space: O(1)
Case insensitive string replacement using Regular Expression
In Python, we can use regular expressions for case-sensitive string replacement.
Python3
import re
test_str = "gfg is BeSt"
repl = "good"
subs = "best"
def replace_substring(test_str, repl, subs):
test_str = re.sub(r '(?i)' + subs, repl, test_str)
return test_str
print ( "The original string is :" , test_str)
print ( "Replaced String :" , replace_substring(test_str, subs, repl))
|
Output
The original string is : gfg is BeSt
Replaced String : gfg is good
Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...