The string type doesn't support this. You're probably best off using the regular expression sub method with the re.IGNORECASE option.
>>> import re
>>> insensitive_hippo = re.compile(re.escape('hippo'), re.IGNORECASE)
>>> insensitive_hippo.sub('giraffe', 'I want a hIPpo for my birthday')
'I want a giraffe for my birthday'
Answer from Blair Conrad on Stack Overflow Top answer 1 of 11
296
The string type doesn't support this. You're probably best off using the regular expression sub method with the re.IGNORECASE option.
>>> import re
>>> insensitive_hippo = re.compile(re.escape('hippo'), re.IGNORECASE)
>>> insensitive_hippo.sub('giraffe', 'I want a hIPpo for my birthday')
'I want a giraffe for my birthday'
2 of 11
117
import re
pattern = re.compile("hello", re.IGNORECASE)
pattern.sub("bye", "hello HeLLo HELLO")
# 'bye bye bye'
w3resource
w3resource.com › python-exercises › re › python-re-exercise-44.php
Python: Do a case insensitive string replacement - w3resource
Write a Python program to do case-insensitive string replacement. ... import re text = "PHP Exercises" print("Original Text: ",text) redata = re.compile(re.escape('php'), re.IGNORECASE) new_text = redata.sub('php', 'PHP Exercises') print("Using 'php' replace PHP") print("New Text: ",new_text) ... Write a Python program to replace a target substring in a string without regard to case.
case sensitive string replacement in Python - Stack Overflow
I need to replace a string in case sensitive way. For example abc -> def Abc -> Def aBc -> dEf abC -> deF What can I do this with Python? More on stackoverflow.com
replace - Case insensitive string replacement in Python - Stack Overflow
To make multiple case insensitive string replacement I'm using regex pattern, e.g. import re text = "Mentioning of reD, GrEen and BLUE is prohibited" new_text = re.sub(r'(red|green|blue)', & More on stackoverflow.com
Replacing Text in a Text File - Case Sensitivity
First, please format your code by adding four spaces before each line of code: find_text = input("enter the word you want to find in this file: ") replace_text = input("enter the word you want to use as a replacement: ") with open('names.txt') as file: data = file.read() if find_text in data: print('Your text has been replaced') else: print('Your initial search term was not found, unable to replace this text') data = data.replace(find_text, replace_text) with open('names.txt', 'w') as file: file.write(data) And to answer your question, simply compare after forcing the text to be of particular casing. str.find will be useful, too, I'm sure. find_text = input("enter the word you want to find in this file: ") replace_text = input("enter the word you want to use as a replacement: ") with open('names.txt') as file: data = file.read() replaced = False while (idx := data.lower().find(find_text.lower())) != -1: data = data[:idx] + replace_text + data[idx+len(find_text):] replaced = True if replaced: print('Your text has been replaced') else: print('Your initial search term was not found, unable to replace this text') with open('names.txt', 'w') as file: file.write(data) More on reddit.com
Case insensitive string comparisons. Know how to do it, really don't like it, deciding if it can be refined or if it's just typical developer being too opinionated.
I don’t have an answer to your question. I just want to add that you should use string.Equals(myString…) rather than myString.Equals() as that handles the case where myString is null. And yes, that makes it even more verbose 🙃 More on reddit.com
03:01
How To Ignore Case In Python String Comparisons? - Python Code ...
00:48
How to iGnOre cAsE in Python strings 🐍 - YouTube
13:44
Python Tips and Tricks: Case-Insensitive String Comparisons Done ...
01:35
How to Efficiently Replace Characters in a String with Case-In...
06:53
Replace string method is case sensitive - String Methods - Python ...
07:33
Case Insensitive Matching in Python with .Casefold() - YouTube
GeeksforGeeks
geeksforgeeks.org › python › python-case-insensitive-string-replacement
Case insensitive string replacement in Python - GeeksforGeeks
July 23, 2025 - 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. ... 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) ... Explanation: re.compile(re.escape(b), re.IGNORECASE) compiles a case-insensitive regex pattern from the target word b.
TutorialsPoint
tutorialspoint.com › article › case-insensitive-string-replacement-using-python-program
Case-insensitive string replacement using Python Program
March 26, 2026 - 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)
Finxter
blog.finxter.com › 5-best-ways-to-perform-case-insensitive-string-replacement-in-python
5 Best Ways to Perform Case Insensitive String Replacement in Python – Be on the Right Side of Change
February 27, 2024 - It then defines a pattern to look for in the text, a replacement string, and uses the re.sub() function, passing the re.IGNORECASE flag to replace all case insensitive occurrences of the pattern with the replacement string. This method involves converting both the string and the substring to ...
CodeVsColor
codevscolor.com › write a python program to do a case insensitive string replacement - codevscolor
write a python program to do a case insensitive string replacement - CodeVsColor
July 23, 2020 - The program will take the string and the sub-string to replace as inputs from the user. case-insensitive string replacement doesn’t consider any cases while doing the replacement. For example, if the string is World, worlD and WORLD both will match this string irrespective of the character cases. We will take the help of regex module re to do the replacement. re module is used for regular expression in python. We will use the sub method of this module. sub is used to replace substrings in a string.
Iditect
iditect.com › programming › python-example › python-case-insensitive-string-replacement.html
Python - Case insensitive string replacement
The re.sub() method using the re module is a more efficient and concise way to perform case-insensitive replacements in strings, especially for longer strings or when the target substring occurs many times.
Medium
medium.com › paulacy-pulse › python-case-insensitive-string-replacement-in-python-6a8abc6d8035
PYTHON — Case-Insensitive String Replacement in Python | by Laxfed Paulacy | Straight Bias Devs
March 5, 2024 - We’ll start by importing the necessary libraries and then demonstrate how to perform case-insensitive string replacement. Create a new Python file, such as case_insensitive_replace.py, and import the regex library. ... Let’s define a function that replaces all occurrences of a substring in a string, ignoring the case.
Real Python
realpython.com › lessons › replace-string-python-case-insensitive
Be Case-Insensitive (Video) – Real Python
And that’s something you may remember from the .replace() method. 00:52 You really need to be specific what you are looking for. In the regular expression input string, you have a lowercase string with blast, but in your test string, there is either BLASTED in uppercase or Blast with a uppercase character.
Published: August 22, 2023
Mycoding
mycoding.uk › a › python__how_to_do_case_insensitive_string_replacement.html
Python: How to do Case insensitive string replacement
Also, we will pre-compile search patter into regular expression for future use with ignoring case · import re # import module for regular expressions test_string = 'One two oNe Two onE tWo ONE TWO one tWO' # string to be replaces pattern = 'two' # pattern for seach replace = '222' # patter for replacement #replace = 'TWO' compiled = re.compile(re.escape(pattern), re.IGNORECASE) # pre-compiled pattern new_string = compiled.sub(replace, test_string) # replace with pre-compiled pattern print( new_string) # One 222 oNe 222 onE 222 ONE 222 one 222
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.0 › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 0.25.0 documentation
Series.str.replace(self, pat, repl, n=-1, case=None, flags=0, regex=True)[source]¶ · Replace occurrences of pattern/regex in the Series/Index with some other string.
YouTube
youtube.com › lazy tutorials
Search and Replace Case Insensitive Text - Python Recipe - YouTube
This tutorial explains how to search and replace case insensitive text in Python using real world examples.
Published: April 27, 2018
Views: 974
Top answer 1 of 7
8
from string import maketrans
"Abc".translate(maketrans("abcABC", "defDEF"))
2 of 7
7
Expanding on Mark Byers' answer, Here's a solution which works for replacement text of any length.
The trick is to send a function to re.sub().
import re
def case_sensitive_replace(string, old, new):
""" replace occurrences of old with new, within string
replacements will match the case of the text it replaces
"""
def repl(match):
current = match.group()
result = ''
all_upper=True
for i,c in enumerate(current):
if i >= len(new):
break
if c.isupper():
result += new[i].upper()
else:
result += new[i].lower()
all_upper=False
#append any remaining characters from new
if all_upper:
result += new[i+1:].upper()
else:
result += new[i+1:].lower()
return result
regex = re.compile(re.escape(old), re.I)
return regex.sub(repl, string)
print case_sensitive_replace("abc Abc aBc abC ABC",'abc','de')
print case_sensitive_replace("abc Abc aBc abC ABC",'abc','def')
print case_sensitive_replace("abc Abc aBc abC ABC",'abc','defg')
Result:
de De dE de DE
def Def dEf deF DEF
defg Defg dEfg deFg DEFG
Chryswoods
chryswoods.com › beginning_python › replacing.html
chryswoods.com | Search and Replace
You can also use variables in the search and replace parts of the substitute string, e.g. import re search = "the" replace = "THE" line = "The THEsis is the thEory of Theocracy" #case-insensitive replace "the" with "THE" line = re.sub(search, replace, line, flags=re.IGNORECASE) print(line)
Iditect
iditect.com › faq › python › case-insensitive-replace-in-python.html
Case insensitive replace in python
new_string = original_string.replace('old_string', 'new_string', -1) Performing case insensitive replace in Python without regular expressions