Python — check if a string contains Cyrillic characters

Max Malysh picture Max Malysh · Jan 15, 2018 · Viewed 10k times · Source

How to check whether a string contains Cyrillic characters?

E.g.

>>> has_cyrillic('Hello, world!')
False
>>> has_cyrillic('Привет, world!')
True

Answer

Max Malysh picture Max Malysh · Jan 15, 2018

You can use a regular expression to check if a string contains characters in the а-я, А-Я range:

import re 

def has_cyrillic(text):
    return bool(re.search('[а-яА-Я]', text))

Alternatively, you can match the whole Cyrillic script range:

def has_cyrillic(text):
    return bool(re.search('[\u0400-\u04FF]', text))

This will also match letters of the extended Cyrillic alphabet (e.g. ё, Є, ў).