Search code examples
stringpython-2.7listuppercaselowercase

Checking for an uppercase in strings inside a list


I have been tasked to do a small mission for a list that contains only strings I need to check the following and create a list if there is a capital letter in the string I need to isolate that word and put it in the new list as a separate word, for example for the list

str_list = [“This”, “is”, “not a Red”, “”, “wine, but a white One”]

the new one needs to look like this

split_str_list = [“This”, “is”, “not a ”, ”Red”, “”, “wine, but a white “, ”One”]

thank you very much for the help


Solution

  • str_list = ["This","is","not a Red","","Wine, but a white One"]
    new_list=[]
    string=''
    for i in str_list:
        print i
        words=i.split(' ')
        print words
        for x in words:
            if x == ' ' or x=='':
                new_list.append(x)
    
            elif x[0].isupper() and string != '':
                string=string.strip()
                new_list.append(string)
                new_list.append(x)
                string=''
            elif x[0].isupper() and string == '':
                new_list.append(x)
    
            else:
                string=string+x+' '
    print new_list
    

    OUTPUT:['This', 'is not a', 'Red', '', 'Wine,', 'but a white', 'One']