'How do you make conditional statements using different languages such as Arabic in Python?

So I'm trying to loop through an Arabic string then if the letter is a certain value the code does something so the conditional statement would look something like this:

if a[i]=="و":
    # Expecting to get this executed but it doesn't even when the letter is same
    # Further logic down ahead.

The only issue is that the conditional doesn't run even when the value is that letter. How would I go round doing this?



Solution 1:[1]

Check if your editor supports utf-8. The code below works well for me:

a = 'abc?def'

for i in range(len(a)):
    if a[i] == '?':
        print(f"'{a[i]}' match")

Output:

'?' match

Solution 2:[2]

You could try to convert those into unicode code number and then compare it like below with the help of ord inbuilt function in python. It also returns True when the condition is met.

given_letter = '?'
print(ord(given_letter[0]) ==  ord('?'))
True

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien
Solution 2 Alonso