Curious to see if there's a way to return all capital letters from a string in Python, not using is.upper, maybe using other conditionals?
Asked
Active
Viewed 2,136 times
0
-
1Why not using `isupper`? Otherwise `return [i for i in s if i in string.ascii_uppercase`] would work – Cory Kramer Oct 22 '17 at 14:35
-
1Or if you want to impress her: `re.sub('[^A-Z]', '', your_string)` – Maroun Oct 22 '17 at 14:36
-
2@cᴏʟᴅsᴘᴇᴇᴅ `isupper` is not a function applied to a astring, but a method. That filter statement won't work. – user2390182 Oct 22 '17 at 14:41
2 Answers
1
Without using anything other than list comprehension
(that too can be replaced by normal for
loop)
>>> [s for s in string if 'A'<=s<='Z']
=> ['A', 'D', 'F', 'G']
#driver values :
IN : string = 'AbcDeFGh2i'

Kaushik NP
- 6,733
- 9
- 31
- 60