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
🌐
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.
Discussions

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
🌐 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
🌐 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
🌐 r/learnpython
5
2
February 7, 2022
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
🌐 r/csharp
18
1
December 12, 2021
🌐
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 ...
🌐
freeCodeCamp
freecodecamp.org › news › python-string-replace-function-in-python-for-substring-substitution
Python String.Replace() – Function in Python for Substring Substitution
January 24, 2022 - In order to perform a case-insensitive substring substitution you would have to do something different. You would need to use the re.sub() function and use the re.IGNORECASE flag. ... Use the re module, via import re.
🌐
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.
Find elsewhere
🌐
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.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › how to use python replace function
How to use Python Replace function - Shiksha Online
October 26, 2023 - str.replace (old, new, count) is ... string. replace () function is case-sensitive, i.e., it replaces a perfectly matching substring, and it will return the old string if the given substring does not find in the string...
🌐
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
🌐
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)
🌐
Leapcell
leapcell.io › blog › understanding-python-replace-method-for-string-manipulation
Understanding Python's `replace()` Method for String Manipulation | Leapcell
July 25, 2025 - Case Sensitivity: As noted earlier, replace() is case-sensitive. To perform case-insensitive replacements, you might need to use regular expressions with the re module or convert the string to a consistent case before performing replacements.
🌐
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
🌐
Medium
medium.com › paulacy-pulse › python-python-replace-string-case-insensitive-ebf89d3fde55
PYTHON — Python Replace String Case Insensitive | by Laxfed Paulacy | Straight Bias Devs
December 1, 2024 - Let’s start by writing code that demonstrates how to replace a string in a case-insensitive manner using the re module. import re # Sample input string input_string = "Python is a popular programming language.