Skip to content Skip to sidebar Skip to footer

How To Check If String Is 100% Ascii In Python 3

i have two strings eng = 'Clash of Clans – Android Apps on Google Play' rus = 'Castle Clash: Новая Эра - Android Apps on Google Play' and now i want to check whether str

Solution 1:

As with Salvador Dali's answer you linked to, you must use a try-catch block to check for an error in encoding.

# -*- coding: utf-8 -*-defisEnglish(s):
    try:
        s.encode('ascii')
    except UnicodeEncodeError:
        returnFalseelse:
        returnTrue

Just to note though, when I copy and pasted your eng and rus strings to try them, they both came up as False. Retyping the English one returned True, so I'm not sure what's up with that.

Solution 2:

Your English string really isn't true ASCII, it contains the character U+2013 - EN DASH. This looks very similar to the ASCII dash U+002d but it is different.

If this is the only character you need to worry about, you can do a simple replacement to make it work:

>>> eng.replace('\u2013', '-').encode('ascii')
b'Clash of Clans - Android Apps on Google Play'

Solution 3:

You can use the isascii() method:

>>> rus.isascii()
False

Post a Comment for "How To Check If String Is 100% Ascii In Python 3"