17
votes
#input
my_string = 'abcdefgABCDEFGHIJKLMNOP'

how would one extract all the UPPER from a string?

#output
my_upper = 'ABCDEFGHIJKLMNOP'
7
can you show us what you have tried so far? - user4815162342
i found a way to do it with a for loop but it did not seem to efficient - O.rka
please do provide it in the question; it is far from obvious what kind of efficiency constraints you are facing - user4815162342
i'm stil getting used to the join function but i knew it would be something along those lines - O.rka
When you say "it did not seem too efficient", what do you mean? You tested it and it was too slow? You suspect there's some quadratic behavior somewhere when it should be linear? Or…? - abarnert

7 Answers

34
votes

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'
7
votes
import string
s = 'abcdefgABCDEFGHIJKLMNOP'
s.translate(None,string.ascii_lowercase)

string.translate(s, table[, deletechars]) function will delete all characters from the string that are in deletechars, a list of characters. Then, the string will be translated using table (we are not using it in this case).

To remove only the lower case letters, you need to pass string.ascii_lowercase as the list of letters to be deleted.

The table is None because when the table is None, only the character deletion step will be performed.

6
votes

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".

2
votes

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
2
votes

You could use a more functional approach

>>> s = 'abcdefgABCDEFGHIJKLMNOP'
>>> filter(str.isupper, s)
'ABCDEFGHIJKLMNOP'
0
votes

here you go:

my_string = 'abcdefgABCDEFGHIJKLMNOP'

cleanChar = ''

for char in my_string:
    if char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        cleanChar = cleanChar + char

newChar = cleanChar
print(" {}".format(newChar))
0
votes
for char in my_string:
     if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
         print(char)