Jul-21-2019, 05:42 PM
This should work.
You could write the function as generator:
lista =[] def capital_letters(arg): for i in arg: if i.isupper(): lista.append(i) # append the i to the list return lista # return the list after the for-loop is done print(capital_letters('tZtTtT'))
arg
, i
and lista
is a bad name.You could write the function as generator:
def filter_capital_letters(text): for char in text: if char.isupper(): yield char # the yield statement makes a generator # the generator must be consumed list(filter_capital_letters('tZtTtT'))Or you could write it as list comprehension or generator expression.
text = 'Hello World' upper_chars = [char for char in text if char.isupper()] upper_chars_genetator = (char for char in text if char.isupper()) # upper_chars_generator does nothing until it's consumed by # a list/tuple or in a for-loop # this interesting for data, which does not fit in memory.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!