Python — check if a string contains Cyrillic characters

前端 未结 4 1942
一整个雨季
一整个雨季 2020-12-31 06:19

How to check whether a string contains Cyrillic characters?

E.g.

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


        
4条回答
  •  悲&欢浪女
    2020-12-31 07:15

    You could create a set containing the cyrillic letters and just check each character of the string:

    cyrillic_letters = {....} # fill it with the cyrillic letters
    
    def has_cyrillic(text):
        for c in text:
            if c in cyrillic_letters:
                return True
        return False
    

提交回复
热议问题