Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python — check if a string contains Cyrillic characters

How to check whether a string contains Cyrillic characters?

E.g.

>>> has_cyrillic('Hello, world!')
False
>>> has_cyrillic('Привет, world!')
True
like image 583
Max Malysh Avatar asked Jan 14 '18 23:01

Max Malysh


People also ask

How do you check if a string contains an alphabet in Python?

Python String isalpha() The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.

How do you check if a string has any letters?

To check if a string contains any letters, use the test() method with the following regular expression /[a-zA-Z]/ . The test method will return true if the string contains at least one letter and false otherwise.


2 Answers

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. ё, Є, ў).

like image 110
Max Malysh Avatar answered Oct 10 '22 00:10

Max Malysh


regex supports Unicode properties, along with a few short forms.

>>> regex.search(r'\p{IsCyrillic}', 'Hello, world!')
>>> regex.search(r'\p{IsCyrillic}', 'Привет, world!')
<regex.Match object; span=(0, 1), match='П'>
>>> regex.search(r'\p{IsCyrillic}', 'Hello, wёrld!')
<regex.Match object; span=(8, 9), match='ё'>
like image 30
Ignacio Vazquez-Abrams Avatar answered Oct 10 '22 00:10

Ignacio Vazquez-Abrams