问题
Why does u'\u02c7'.isalpha()
return True, if symbol ˇ
is not alphabetic? Does this method work properly only with ASCII chars?
回答1:
U+02c7 CARON is a codepoint in the Lm (Modifier Letter) category, so according to the Unicode standard, it is alphabetic.
The documentation for str.isalpha()
makes it clear what is included:
Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”.)
You didn't define what you mean by work properly; clearly you have a different definition of what constitutes an alphabetic letter. If you only expected Latin-1 letters, then you need to limit also need to test if the string can be encoded safely to Latin-1. There are exactly zero Lm-category codepoints in the Latin-1 subset of Unicode (and no Lt characters either, and only 2 Lo characters, ª (U+00AA) and º (U+00BA)).
来源:https://stackoverflow.com/questions/49598702/python-isalpha-returns-true-on-unicode-modifiers