Skip to content Skip to sidebar Skip to footer

How Do I Make This Character Counter Be Insensitive To Case?

I wrote this little code to count occurrences of words in a text: string=input('Paste text here: ') word=input('Type word to count: ') string.count(word) x=string.count(word) print

Solution 1:

Convert both the text and the word you're searching for to uppercase.

string.upper().count(word.upper())

Since strings are immutable, it won't permanently change the text or the word.

Solution 2:

Use .lower() or .upper() to convert your inputs to all uppercase or lowercase

string=input("Paste text here: ").lower()
word=input("Type word to count: ").lower()
string.count(word)
x=string.count(word)
print (x)

Post a Comment for "How Do I Make This Character Counter Be Insensitive To Case?"