Ray*_*ger 149
字符串上有许多"is methods".islower()并isupper()应满足您的需求:
>>> 'hello'.islower()
True
>>> [m for m in dir(str) if m.startswith('is')]
['isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper']
Run Code Online (Sandbox Code Playgroud)
以下是如何使用这些方法对字符串列表进行分类的示例:
>>> words = ['The', 'quick', 'BROWN', 'Fox', 'jumped', 'OVER', 'the', 'Lazy', 'DOG']
>>> [word for word in words if word.islower()]
['quick', 'jumped', 'the']
>>> [word for word in words if word.isupper()]
['BROWN', 'OVER', 'DOG']
>>> [word for word in words if not word.islower() and not word.isupper()]
['The', 'Fox', 'Lazy']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
120793 次 |
| 最近记录: |