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 (x)
The problem is that it is case sensitive. How can I make it be case insensitive?
In this, we perform lower() to all the strings, before mapping in defaultdict. This ensures case insensitivity while mapping and cumulating frequency.
Approach No 1: Python String lower() Method This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings.
The most basic way to do case insensitive string comparison in JavaScript is using either the toLowerCase() or toUpperCase() method to make sure both strings are either all lowercase or all uppercase.
All versions of HTML including HTML5 are case insensitive except XHTML. HTML, being case-insensitive language, means whether you write a tag or an attribute in lowercase letters or uppercase letters or both, it will be treated as the same. We can also mix the cases in a single tag or attribute name as well.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With