m0_57644467 2021-11-04 23:31 采纳率: 0%
浏览 397

python编写一段程序,实现纠正一段英文中的两种错误

编写一段程序,实现纠正一段英文中的两种错误:(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题执行结果图

img

  • 写回答

1条回答 默认 最新

  • 关注

    用正则表达式替换

    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)
    

    img

    如有帮助,望采纳!谢谢!

    评论

报告相同问题?

问题事件

  • 修改了问题 11月5日
  • 创建了问题 11月4日