首页 > 解决方案 > 如何使这个大小写敏感?

问题描述

我不确定如何使结果显示单词是大写还是小写。当我更改字母时,我意识到输出不再相同。

filename = 'Pride_and_Prejudice_Chapter 1.txt'

def count_common_words(filename, word):
    try:
        with open(filename) as f:
            contents = f.read()
    except FileNotFoundError:
        pass
    else:
        word_count = contents.lower().count(word)

        msg = f"'{word}' appears in {filename} about {word_count} times."
        print(msg)

count_common_words(filename, 'the')
count_common_words(filename, 'you')
count_common_words(filename, 'i')

标签: google-colaboratory

解决方案


推荐阅读