string - Python string case-insensitive replacement
伊谢尔伦
伊谢尔伦 2017-06-28 09:24:52
0
2
1554

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?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
仅有的幸福

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'
阿神
import re

s = 'Hello World, HELLO PYTHON'
print re.sub(r'(?i)hello', 'My', s)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!