Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Case-insensitive string replacement using Python Program
Case-insensitive string replacement is a common text processing task where you need to replace substrings regardless of their letter case. Python provides several approaches using regular expressions, string methods, and list comprehensions.
Using re.IGNORECASE with re.compile()
The regex module provides re.IGNORECASE flag to ignore case differences during pattern matching ?
import re
# input string
input_string = "Hello TutorialsPOINT Python"
print("Input String:", input_string)
# substring to be replaced and replacement
substring = "tutorialspoint"
replace_string = "Java"
# compile regex with IGNORECASE flag
compiled_pattern = re.compile(re.escape(substring), re.IGNORECASE)
# perform case-insensitive replacement
result = compiled_pattern.sub(replace_string, input_string)
print("Result:", result)
Input String: Hello TutorialsPOINT Python Result: Hello Java Python
Using re.sub() with Inline Flags
You can use the (?i) inline flag for case-insensitive matching without compiling ?
import re
input_string = "Hello TutorialsPOINT Python"
substring = "tutorialspoint"
replace_string = "Java"
# use (?i) for case-insensitive matching
result = re.sub('(?i)' + re.escape(substring), replace_string, input_string)
print("Result:", result)
Result: Hello Java Python
Using String Methods with split()
Split the string into words, compare each word in lowercase, and replace matches ?
input_string = "Hello TutorialsPOINT Python"
substring = "tutorialspoint"
replace_string = "Java"
words = input_string.split()
result_words = []
for word in words:
if word.lower() == substring.lower():
result_words.append(replace_string)
else:
result_words.append(word)
result = " ".join(result_words)
print("Result:", result)
Result: Hello Java Python
Using List Comprehension
A more concise approach using list comprehension with enumerate() ?
input_string = "Hello TutorialsPOINT Python"
substring = "tutorialspoint"
replace_string = "Java"
words = input_string.split()
result_words = [replace_string if word.lower() == substring.lower() else word for word in words]
result = " ".join(result_words)
print("Result:", result)
Result: Hello Java Python
Comparison of Methods
| Method | Best For | Performance | Complexity |
|---|---|---|---|
re.compile() |
Multiple replacements | Fast for repeated use | Medium |
re.sub() with (?i)
|
Single replacements | Good | Low |
| String methods | Word-based replacement | Good for small strings | Low |
| List comprehension | Functional programming style | Good | Low |
Conclusion
Use re.sub() with (?i) flag for simple case-insensitive replacements. For complex patterns or multiple operations, re.compile() with re.IGNORECASE is more efficient. String methods work well for word-based replacements.
