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'
String Equals Check in Python Example: s1 = 'String' s2 = 'String' s3 = 'string' # case sensitive equals check if s1 == s2: print('s1 and s2 are equal. ') if s1. __eq__(s2): print('s1 and s2 are equal.
We can remove accents from the string by using a Python module called Unidecode. This module consists of a method that takes a Unicode object or string and returns a string without ascents.
Python strings equality can be checked using == operator or __eq__() function. Python strings are case sensitive, so these equality check methods are also case sensitive.
String Comparison using == in PythonThe == function compares the values of two strings and returns if they are equal or not. If the strings are equal, it returns True, otherwise it returns False.
You should use unidecode
function from Unidecode package:
from unidecode import unidecode
if unidecode(u'séquoia') in 'Mon sequoia est vert':
print 'Bonjour'
You should take a look at Unidecode. With the module and this method, you can get a string without accent and then make your comparaison:
def remove_accents(data):
return ''.join(x for x in unicodedata.normalize('NFKD', data) if x in string.ascii_letters).lower()
if remove_accents('séquoia') in 'Mon sequoia est vert':
# Do something
pass
Reference from stackoverflow
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