84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
ReplacehelloinHello World, HELLO PYTHONwithMy.Since the replace() function replacement is case-sensitive, how can Python implement string replacement without case sensitivity?
hello
Hello World, HELLO PYTHON
My
小伙看你根骨奇佳,潜力无限,来学PHP伐。
Reference article: Issues related to Python string operations
String case-insensitive replacementstr.replace(old, new[, max])The replacement is case-sensitive. Case-insensitive replacement requires the regular expressionre.sub() with there.IGNORECASEoption.
str.replace(old, new[, max])
re.sub(
re.IGNORECASE
>>> import re >>> reg = re.compile(re.escape('hello'), re.IGNORECASE) >>> reg.sub('My', 'Hello World, HELLO PYTHON') 'My World, My PYTHON'
import re s = 'Hello World, HELLO PYTHON' print re.sub(r'(?i)hello', 'My', s)
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.