-2

How to make the case insensitive the result of a search entered with an input on a text. For example if I enter "Poésie" he will find But if I enter "poésie" he will not find.

I do not know how to use the Regex Re.ignorecase method for the INPUT variable

here is a simple example

import os
rep_cour = os.getcwd()

file = open(rep_cour+"/data/testmultiple/text1.txt","r")


search_word = input("enter a word you want to search in file: ")
if(search_word in file.read()):
    print("word found")
else:
    print("word not found") 

thank you

edit : With the casefold() method it works

I found the solution as suggested by StarckOverflow here :

str.casefold is recommended for case-insensitive string matching

Zembla
  • 331
  • 1
  • 2
  • 9

1 Answers1

1

You need to convert both search_word and file.read() to lower or upper. And then compare it as,

search_word.lower() in file.read().lower()
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
  • thank you. Your solution works if I write everything in a lowercase character. But I would like the result of the search to be insensitive to the case – Zembla Jan 27 '22 at 11:40