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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-case-insensitive-string-replacement
Case insensitive string replacement in Python - GeeksforGeeks
July 23, 2025 - 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 ...
Discussions

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 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
regex - Case-insensitive string replacement in python - Stack Overflow
I am able to replace a word in a string content using the following solution in a case insensitive method More on stackoverflow.com
🌐 stackoverflow.com
June 26, 2012
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
🌐
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 - The re.IGNORECASE flag tells the regular expression to perform a case-insensitive match. ... import re phrase = "I am learning Ruby. I really enjoy the ruby programming language!" phrase = re.sub("Ruby","Python", phrase, flags=re.IGNORECASE) ...
🌐
w3resource
w3resource.com › python-exercises › re › python-re-exercise-44.php
Python: Do a case insensitive string replacement - w3resource
Write a Python program to replace all occurrences of a substring regardless of case and then print the modified string. Write a Python function to implement case-insensitive string replacement using regex flags.
🌐
TutorialsPoint
tutorialspoint.com › article › case-insensitive-string-replacement-using-python-program
Case-insensitive string replacement using Python Program
March 26, 2026 - input_string = "Hello TutorialsPOINT ... = " ".join(result_words) print("Result:", result) ... Use re.sub() with (?i) flag for simple case-insensitive replacements....
🌐
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
🌐
Real Python
realpython.com › lessons › replace-string-python-case-insensitive
Be Case-Insensitive (Video) – Real Python
01:21 Currently, you have global and multi line activated, and that’s why your multiline string works as a test string, but you haven’t selected insensitive so that you look for case-insensitive matches. So once you select it, you will see that the regular expression successfully now finds BLASTED in uppercase or Blast with a uppercase B at the beginning.
Published: August 22, 2023
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › replacing text in a text file - case sensitivity
r/learnpython on Reddit: Replacing Text in a Text File - Case Sensitivity
February 7, 2022 -

If I have a text file(let's say names.txt) that has a ton of names in it, if I want to replace all versions of a name I input how do I get it to ignore the case insensitivity? Example: I have Sarah in the document as Sarah, SARAH, SaRah, and sarah. How do I replace all with Mike without having to individually input all versions for replacement? Here's what I've written so far:

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(r'names.txt', 'r') 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(r'names.txt', 'w') as file:

file.write(data)

🌐
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 - For instance, replacing “python” with “Java” in the string “I love Python, PyThon is great!” should result in “I love Java, Java is great!”. How do we reliably perform this operation in Python? The re module in Python provides regular expression matching operations. To perform a case insensitive string replacement, we can use the re module’s sub() function alongside the re.IGNORECASE flag.
🌐
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 - We will pass re.IGNORECASE as flags, that will do the replacement case-insensitive. Below is the python program to do case insensitive string replacement :
🌐
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
🌐
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.
🌐
Chryswoods
chryswoods.com › beginning_python › replacing.html
chryswoods.com | Search and Replace
Case-insensitive substitution is a little more complex. For starters you will have to use the re module again. It provides a function called sub() (short for ‘substitute’) which works in a similar way to re.search() except that now there is an extra argument that is the replacement string.
🌐
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 ... 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....
🌐
Iditect
iditect.com › faq › python › case-insensitive-replace-in-python.html
Case insensitive replace in python
To perform a case-insensitive replace in Python, you can use regular expressions from the re module with the re.IGNORECASE flag.
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-perform-case-insensitive-string-replace
How to Perform Case-Insensitive String Replace in Python | Tutorial Reference
Python's built-in .replace() method is strictly case-sensitive. To replace "Apple", "apple", and "APPLE" with the same replacement, you need to use regular expressions. The re module provides the re.IGNORECASE flag (or its shorthand re.I) for case-insensitive matching: