Search code examples
pythonlistrangeuppercaselowercase

How to change words in a list to upper/lower case using range - Python


One part of my assignment is to get user input, convert that into a list, then depending on the number of characters in each word, change to upper/lower case accordingly. I have been told I have to use range, which is the part I am struggling with. This is my latest attempt but it's not working. Any advice would be appreciated.

poem = input("Enter a poem, verse or saying: ")
words_list = poem.split()
list_len = len(words_list)
for word in range(0,list_len):
    if len(words_list[word]) < 4:
        word = word.lower()
    elif len(words_list[word]) > 6:
        word = words.upper()

Solution

  • Just a small modification to your original code. Since you want to convert to upper/lower case, you also want to probably save the output. You can alternatively use a new list to save your output rather than replacing the values in the original list words_list

    for word in range(0,list_len):
        if len(words_list[word]) < 4:
            words_list[word] = words_list[word].lower()
        elif len(words_list[word]) > 6:
            words_list[word] = words_list[word].upper()
    
    print(words_list)
    

    Output

    Enter a poem, verse or saying: My name is bazingaa
    ['my', 'name', 'is', 'BAZINGAA']