How to check whether a string contains Cyrillic characters?
E.g.
>>> has_cyrillic('Hello, world!')
False
>>> has_cyrillic('Привет, world!')
True
Python String isalpha() The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.
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.
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. ё, Є, ў).
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='ё'>
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