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

前端 未结 9 1195
无人共我
无人共我 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:39

    You can use isupper method for your purpose:

    text = 'sssample Text with And without'
    
    uppers = []
    lowers = []
    
    # Note that loop below could be modified to skip ,.-\/; and etc if neccessary
    for word in text.split():
        uppers.append(word) if word[0].isupper() else lowers.append(word)
    

    EDITED: You can also use islower method the following way:

    text = 'sssample Text with And without'
    
    other = []
    lowers = []
    
    # Note that loop below could be modified to skip ,.-\/; and etc if neccessary
    for word in text.split():
        lowers.append(word) if word.islower() else other.append(word)
    

    OR depends on what you really need you can take a look at istitle method:

    titled = []
    lowers = []
    
    for word in text.split():
        titled.append(word) if word.istitle() else lower.append(word)
    

    AND with simple if else statement:

    titled = []
    lowers = []
    
    for word in text.split():       
        if word.istitle():
            titled.append(word) 
        else:
            lower.append(word)
    

提交回复
热议问题