Search code examples
pythonuppercase

Making each alternative word lower and upper case


Ask for a string from the user and make each alternative word lower and upper case (e.g. the string “I am learning to code” would become “i AM learning TO code”). Using the split and join functions will help you here.

I did a similar thing for characters in the string, but as I found out it doesn't work with full words.

new_string = input("Please enter a string: ")
char_storage = "" #blank string to store all the string's characters
char = 1

for i in new_string: 
    if char % 2 == 0:
        char_storage += i.lower()
    else:   
        char_storage += i.upper()
    char += 1

print(char_storage)

I am still quite confused about how python connects char with new_string value, if anyone has a good website where it is explained I would be very grateful.


Solution

  • You can try this

    new_string = input("Please enter a string: ")
    char_storage = "" #blank string to store all the string's characters
    char = 1
    
    for i in new_string.split(): 
        if char != 1:
            char_storage += " "
        if char % 2 == 0:
            char_storage += i.lower()
        else:   
            char_storage += i.upper()
        char += 1