Is there a property of characters that returns the case? Like maybe char.case()
? I need this to get rid of some repetitive code that's bothering me.
Asked
Active
Viewed 373 times
2 Answers
3
The functions isupper()
and islower ()
are what you need
>>> 'a'.isupper()
False
>>> 'A'.isupper()
True
>>> 'b'.islower()
True
>>> 'B'.islower()
False

Bhargav Rao
- 50,140
- 28
- 121
- 140
-
Okay, I guess there isn't a `.case()` function. I know of the functions you mentioned, it's just that I wanted to get rid of some repetitive code where I had `if str.upper()` followed by `elif str.lower()`. Thanks, though! – polarbits Jan 24 '15 at 15:48
-
@Barghav Rao I had messed up some formatting and accidentally pressed the enter button. It should be fixed now. – polarbits Jan 24 '15 at 15:50
-
@PolarBearITS You are welcome, Your `elif` can be `else` only, as if `str.upper()` is false, then the other (`str.lower()`)will be correct – Bhargav Rao Jan 24 '15 at 15:52
-
2@BhargavRao not necessarily, for example `'1'.isupper()` and `'1'.islower()` are both `False`. – jonrsharpe Jan 24 '15 at 17:30
-
@jonrsharpe Thanks ... But better if you had tagged the OP so he would have learnt this – Bhargav Rao Jan 24 '15 at 17:32
2
You can use str.isupper()
and str.islower()
>>> 'a'.isupper()
False
>>> 'a'.islower()
True
>>> 'A'.isupper()
True
>>> 'A'.islower()
False

Cory Kramer
- 114,268
- 16
- 167
- 218