how to search for a capital letter within a string and return the list of words with and without capital letters

前端 未结 9 1201
无人共我
无人共我 2020-12-18 23:23

My homework assignment is to Write a program that reads a string from the user and creates a list of words from the input.Create two lists, one containing the words that con

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 23:37

    You can use List Comprehensions to get all upper case characters and lower case characters.

    def get_all_cap_lowercase_list(inputStr):
        cap_temp_list = [c for c in inputStr if c.isupper()]
        low_temp_list = [c for c in inputStr if c.islower()]
        print("List of Cap {0}  and List of Lower {1}".format(cap_temp_list,low_temp_list))
    
        upper_case_count = len(cap_temp_list)
        lower_case_count = len(low_temp_list)
        print("Count of Cap {0}  and Count of Lower {1}".format(upper_case_count,lower_case_count))
    get_all_cap_lowercase_list("Hi This is demo Python program")
    

    And The output is:

    List of Cap ['H', 'T', 'P'] and List of Lower ['i', 'h', 'i', 's', 'i', 's', 'd', 'e', 'm', 'o', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm']

    Count of Cap 3 and Count of Lower 22

提交回复
热议问题