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

ReplacehelloinHello World, HELLO PYTHONwithMy.
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 expressionre.sub() with there.IGNORECASEoption.

>>> 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!