编写一段程序,实现纠正一段英文中的两种错误:(1)单独字母“I”误写为“i”的情况;(2)以及单词中间字母“i”误写为“I”的情况。运行结果如图3所示。【参考代码行数:32行】
测试用短文:
i am a student. I am sIx years old. i like read book. I lIke play the piano.
I am a unIversity student. i major in Information management and informatIon system.
图3第3题执行结果图
用正则表达式替换
import re
s = '''i am a student. I am sIx years old. i like read book. I lIke play the piano.
I am a unIversity student. i major in Information management and informatIon system.'''
s = re.sub(r'\bi\b','I',s)
s = re.sub(r'\BI','i',s)
print(s)
如果Information 单词首字母“I”也要转成“i”
可以这样
s = re.sub(r'\BI|I\B','i',s)
import re
s = '''i am a student. I am sIx years old. i like read book. I lIke play the piano.
I am a unIversity student. i major in Information management and informatIon system.'''
s = re.sub(r'\bi\b','I',s)
s = re.sub(r'\BI|I\B','i',s)
print(s)
如有帮助,望采纳!谢谢!