#input
my_string = \'abcdefgABCDEFGHIJKLMNOP\'
how would one extract all the UPPER from a string?
#output
my_upper = \'ABCDEFGHIJKL
Using list comprehension:
>>> s = 'abcdefgABCDEFGHIJKLMNOP'
>>> ''.join([c for c in s if c.isupper()])
'ABCDEFGHIJKLMNOP'
Using generator expression:
>>> ''.join(c for c in s if c.isupper())
'ABCDEFGHIJKLMNOP
You can also do it using regular expressions:
>>> re.sub('[^A-Z]', '', s)
'ABCDEFGHIJKLMNOP'
here you go:
my_string = 'abcdefgABCDEFGHIJKLMNOP'
cleanChar = ''
for char in my_string:
if char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
cleanChar = cleanChar + char
newChar = cleanChar
print(" {}".format(newChar))
Higher order functions to the rescue!
filter(str.isupper, "abcdefgABCDEFGHIJKLMNOP")
EDIT: In case you don't know what filter does: filter takes a function and an iterable, and then applies the function to every element in the iterable. It keeps all of the values that return true and throws out all of the rest. Therefore, this will return "ABCDEFGHIJKLMNOP".
or use regex ... this is an easy answer
import re
print ''.join(re.findall('[A-Z]+',my_string))
just for comparison
In [6]: %timeit filter(str.isupper,my_list)
1000 loops, best of 3: 774 us per loop
In [7]: %timeit ''.join(re.findall('[A-Z]+',my_list))
1000 loops, best of 3: 563 us per loop
In [8]: %timeit re.sub('[^A-Z]', '', my_list)
1000 loops, best of 3: 869 us per loop
In [10]: %timeit ''.join(c for c in my_list if c.isupper())
1000 loops, best of 3: 1.05 ms per loop
so this join plus findall is the fastest method (per ipython %timeit (python 2.6)) , using a 10000 character long identical string
edit: Or not
In [12]: %timeit my_list.translate(None,string.ascii_lowercase)
10000 loops, best of 3: 51.6 us per loop
for char in my_string:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
print(char)
You could use a more functional approach
>>> s = 'abcdefgABCDEFGHIJKLMNOP'
>>> filter(str.isupper, s)
'ABCDEFGHIJKLMNOP'