简体   繁体   English

如何使这个字符计数器对案例不敏感?

[英]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 (x)

The problem is that it is case sensitive. 问题是它区分大小写。 How can I make it be case insensitive? 如何使其不区分大小写?

Convert both the text and the word you're searching for to uppercase. 将您要搜索的文本和单词转换为大写。

str.upper().count(word.upper())

Since strings are immutable, it won't permanently change the text or the word. 由于字符串是不可变的,因此不会永久更改文本或单词。

Use .lower() or .upper() to convert your inputs to all uppercase or lowercase 使用.lower().upper()将输入转换为全部大写或小写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM