如何检查字符串中的字符是否是字母?蟒蛇 [英] How to check if character in string is a letter? Python

查看:146
本文介绍了如何检查字符串中的字符是否是字母?蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 示例:

s ='abcdefg'
s2 ='123abcd'
s3 ='abcDEFG'

s [0] .islower()= True
s2 [0] .islower()= False
s3 [0] .islower()= True

有没有什么办法可以只是询问它是否是除了做.islower()或.isupper()之外的一个字符?

isalpha(),参见 http://docs.python.org/2/library/stdtypes.html



举例:

 >>> s =a123b
>>>对于s中的char:
... print char,char.isalpha()
...
a True
1 False
2 False
3 False
b True


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?

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() ?

解决方案

You can use isalpha(), see the docs at 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

这篇关于如何检查字符串中的字符是否是字母?蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆