如何检查字符串中的字符是否为字母?(Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15558392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if character in a string is a letter? (Python)
提问by O.rka
I know about islowerand isupper, but can you check whether or not that character is a letter?
For Example:
我知道islowerand isupper,但是你能检查一下那个字符是否是一个字母吗?例如:
>>> s = 'abcdefg'
>>> s2 = '123abcd'
>>> s3 = 'abcDEFG'
>>> s[0].islower()
True
>>> s2[0].islower()
False
>>> s3[0].islower()
True
Is there any way to just ask if it is a character besides doing .islower()or .isupper()?
有没有办法只问它是不是做.islower()或的角色.isupper()?
采纳答案by rainer
You can use str.isalpha().
您可以使用str.isalpha().
For example:
例如:
s = 'a123b'
for char in s:
print(char, char.isalpha())
Output:
输出:
a True
1 False
2 False
3 False
b True
回答by Legolas Bloom
str.isalpha()
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. 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”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.
如果字符串中的所有字符都是字母并且至少有一个字符,则返回 true,否则返回 false。字母字符是在Unicode字符数据库中定义为“字母”的字符,即具有一般类别属性为“Lm”、“Lt”、“Lu”、“Ll”或“Lo”之一的字符。请注意,这与 Unicode 标准中定义的“字母”属性不同。
In python2.x:
在 python2.x 中:
>>> s = u'a1中文'
>>> for char in s: print char, char.isalpha()
...
a True
1 False
中 True
文 True
>>> s = 'a1中文'
>>> for char in s: print char, char.isalpha()
...
a True
1 False
? False
? False
? False
? False
? False
? False
>>>
In python3.x:
在 python3.x 中:
>>> s = 'a1中文'
>>> for char in s: print(char, char.isalpha())
...
a True
1 False
中 True
文 True
>>>
This code work:
此代码工作:
>>> def is_alpha(word):
... try:
... return word.encode('ascii').isalpha()
... except:
... return False
...
>>> is_alpha('中国')
False
>>> is_alpha(u'中国')
False
>>>
>>> a = 'a'
>>> b = 'a'
>>> ord(a), ord(b)
(65345, 97)
>>> a.isalpha(), b.isalpha()
(True, True)
>>> is_alpha(a), is_alpha(b)
(False, True)
>>>
回答by omry
This works:
这有效:
word = str(input("Enter string:"))
notChar = 0
isChar = 0
for char in word:
if not char.isalpha():
notChar += 1
else:
isChar += 1
print(isChar, " were letters; ", notChar, " were not letters.")
回答by MII
I found a good way to do this with using a function and basic code. This is a code that accepts a string and counts the number of capital letters, lowercase letters and also 'other'. Other is classed as a space, punctuation mark or even Japanese and Chinese characters.
我找到了一种使用函数和基本代码来做到这一点的好方法。这是一个接受字符串并计算大写字母、小写字母和“其他”的代码。其他被归类为空格、标点符号甚至日文和中文字符。
def check(count):
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print("There are " + str(lowercase) + " lowercase letters.")
print("There are " + str(uppercase) + " uppercase letters.")
print("There are " + str(other) + " other elements to this sentence.")
回答by Chandan Sharma
data = "abcdefg hi j 12345"
data = "abcdefg hi j 12345"
digits_count = 0
letters_count = 0
others_count = 0
for i in userinput:
if i.isdigit():
digits_count += 1
elif i.isalpha():
letters_count += 1
else:
others_count += 1
print("Result:")
print("Letters=", letters_count)
print("Digits=", digits_count)
Output:
输出:
Please Enter Letters with Numbers:
abcdefg hi j 12345
Result:
Letters = 10
Digits = 5
By using str.isalpha()you can check if it is a letter.
通过使用str.isalpha()您可以检查它是否是一个字母。
回答by amalik2205
This works:
这有效:
any(c.isalpha() for c in 'string')

