Open In App

Case insensitive string replacement in Python

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a string and our task is to replace a specific word in it, ignoring the case of the letters. This means if we want to replace the word “best” with “good”, then all variations like “BeSt”, “BEST”, or “Best” should also be replaced. For example, if the input is “gfg is BeSt”, then the output will be “gfg is good”.

Using re.sub()

This method utilizes Python’s re module to perform a case-insensitive substitution directly. By passing re.IGNORECASE as a flag, it matches the target string regardless of case.It’s a one-liner, making it highly efficient and readable for simple replacements.

Python
import re

a = "gfg is BeSt"
r = re.sub(r"best", "good", a, flags=re.IGNORECASE)  # replace
print(r)

Output
gfg is good

Explanation: re.sub() replaces all occurrences of the word “best” in string a with “good”, ignoring case sensitivity by using the re.IGNORECASE flag and returns the modified string.

Using re.compile()

Here, the regular expression pattern is compiled once with re.IGNORECASE. This improves performance when you’re replacing the same pattern in multiple strings. It escapes special characters using re.escape() to avoid unintended regex behavior.

Python
import re

a = "gfg is BeSt" 
b = "best"  # target
c = "good"  # replace

p = re.compile(re.escape(b), re.IGNORECASE) # pattern
r = p.sub(c, a) # replace
print(r)

Output
gfg is good

Explanation: re.compile(re.escape(b), re.IGNORECASE) compiles a case-insensitive regex pattern from the target word b. p.sub(c, a) then replaces all matches of that pattern in string a with c, and returns the modified string.

Using lambda

This method combines regex replacement with a lambda function for custom logic. The lambda is invoked for every match, enabling dynamic replacement decisions. It allows for advanced handling like transforming the case, prefixing, or conditional replacements.

Python
import re

a = "gfg is BeSt" # input
r = re.sub(r"(?i)best", lambda m: "good", a) # replace
print(r)

Output
gfg is good

Explanation: re.sub(r”(?i)best”, lambda m: “good”, a) uses a case-insensitive regex ((?i) inline flag) to match the word “best” in any casing within the string a, and replaces each match using a lambda function that returns “good”.

Using split

This method splits the string into individual words using split(). Each word is compared in lowercase to the target word, and replaced if it matches. It performs a word-by-word substitution, making it simple but very limited.

Python
a = "gfg is BeSt" 
b = "best" # target
c = "good" #replace

words = a.split()
r = ' '.join([c if w.lower() == b else w for w in words])
print(r)

Output
gfg is good

Explanation: a.split() breaks the string into words, then a list comprehension compares each word in lowercase to the target b (“best”). If it matches, it replaces it with c (“good”); otherwise, it keeps the original word. ‘ ‘.join(…) combines the words back into a single string stored in r.



Next Article
Practice Tags :

Similar Reads