Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List capital letters
#3
This should work.

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!
Reply


Messages In This Thread
List capital letters - by ricardodepaula - Jul-21-2019, 05:09 PM
RE: List capital letters - by Yoriz - Jul-21-2019, 05:27 PM
RE: List capital letters - by DeaD_EyE - Jul-21-2019, 05:42 PM
RE: List capital letters - by ndc85430 - Jul-21-2019, 05:43 PM
RE: List capital letters - by DeaD_EyE - Jul-21-2019, 06:53 PM
RE: List capital letters - by ricardodepaula - Jul-21-2019, 11:35 PM
RE: List capital letters - by ndc85430 - Jul-22-2019, 05:37 AM
RE: List capital letters - by DeaD_EyE - Jul-22-2019, 06:45 AM
RE: List capital letters - by ricardodepaula - Jul-23-2019, 03:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 3,047 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Pynput - no capital letters jmair 2 4,618 Feb-12-2019, 09:53 PM
Last Post: jmair
  make a list of string in capital by using a function anancba 3 3,496 Nov-23-2017, 06:42 AM
Last Post: heiner55
  Ending loop with string (capital & lowercase) MattWilk97 3 3,495 Oct-22-2017, 09:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020