Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make this character counter be insensitive to case?

Tags:

python

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?

like image 420
Sbogorot Knulian Avatar asked Mar 24 '15 20:03

Sbogorot Knulian


People also ask

How do you make a case-insensitive count in Python?

In this, we perform lower() to all the strings, before mapping in defaultdict. This ensures case insensitivity while mapping and cumulating frequency.

How do you use case-insensitive in Python?

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.

How do I make JavaScript not case-sensitive?

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.

How do you ignore case-sensitive in HTML?

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.


1 Answers

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.

like image 68
chenjesu Avatar answered Oct 09 '22 10:10

chenjesu