pythonstring

Compare 2 strings without considering accents in Python


I would like to compare 2 strings and have True if the strings are identical, without considering the accents.

Example : I would like the following code to print 'Bonjour'

if 'séquoia' in 'Mon sequoia est vert':
    print 'Bonjour'

Solution

  • You should use unidecode function from Unidecode package:

    from unidecode import unidecode
    
    if unidecode(u'séquoia') in 'Mon sequoia est vert':
        print 'Bonjour'