So I know about islower and isupper, but i can't seem to find out if you can check whether or not that character is a letter?
所以我知道islower和isupper,但我似乎无法找出你是否可以检查这个字符是否是一个字母?
Example:
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()之外,还有什么方法可以问它是否是一个角色?
3 个解决方案
#1
69
You can use isalpha()
, see the docs at http://docs.python.org/2/library/stdtypes.html
您可以使用isalpha(),请参阅http://docs.python.org/2/library/stdtypes.html上的文档
An example:
一个例子:
>>> s = "a123b"
>>> for char in s:
... print char, char.isalpha()
...
a True
1 False
2 False
3 False
b True
#2
12
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”,“L1”或“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)
>>>
#3
-2
this code works:
这段代码有效:
str=raw_input("enter a string:")
for char in word:
if not char.isalpha():
sum=sum+1
if sum>0:
print char
#1
69
You can use isalpha()
, see the docs at http://docs.python.org/2/library/stdtypes.html
您可以使用isalpha(),请参阅http://docs.python.org/2/library/stdtypes.html上的文档
An example:
一个例子:
>>> s = "a123b"
>>> for char in s:
... print char, char.isalpha()
...
a True
1 False
2 False
3 False
b True
#2
12
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”,“L1”或“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)
>>>
#3
-2
this code works:
这段代码有效:
str=raw_input("enter a string:")
for char in word:
if not char.isalpha():
sum=sum+1
if sum>0:
print char