Replace hello in Hello World, HELLO PYTHON with My. Since the replace() function replacement is case-sensitive, how can Python implement string replacement without case sensitivity?
Reference article: Issues related to Python string operations
String case-insensitive replacement str.replace(old, new[, max])The replacement is case-sensitive. Case-insensitive replacement requires the regular expression re.sub() with the re.IGNORECASE option.
>>> import re
>>> reg = re.compile(re.escape('hello'), re.IGNORECASE)
>>> reg.sub('My', 'Hello World, HELLO PYTHON')
'My World, My PYTHON'
Reference article: Issues related to Python string operations
String case-insensitive replacement
str.replace(old, new[, max])
The replacement is case-sensitive. Case-insensitive replacement requires the regular expressionre.sub(
) with there.IGNORECASE
option.