Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Capitalizing every other letter in a string -- Explain in detail?

I know that this works.. but I don't know how and why it works. If someone can break this down for me, I'd really appreciate it.

def spongebob(string):
    new_quote = ""
    i = True  # capitalize
    for char in string:
        if i:
            new_quote += char.upper()
        else:
            new_quote += char.lower()
        if char != ' ':
            i = not i
    return new_quote
print(spongebob("That's not how you do that!"))

1 Answer

Does adding some comments and print statements help to explain the processing - Try testing some different scenarios. Also notice that the function handles spaces but doesn't do anything with the apostrophe in your sample - I just used small strings to keep it short.

Here's the code:

def spongebob(string):
    # Initialize new quote to an empty string
    new_quote = ""
    # Initialize i to True - do the char.upper() method
    i = True  # capitalize
    # Loop the passed in string
    for char in string:
        print('Begin of loop, i is {}, char is {} and the new quote is {}'.format(i, char, new_quote))
        # When i is true set to upper
        if i:
            # append the upper case character
            new_quote += char.upper()
        # When i is false set to lower
        else:
            # append the lower case character
            new_quote += char.lower()
        # Skip if the char is a space
        if char != ' ':
            # Toggle i - to impact the upper/lower if above
            i = not i
    print("\nAfter Loop, i is {} and the new quote is {}\n".format(i, new_quote))
    return new_quote


print('Run 1 result --->' + spongebob("A B CD"))
print("\nOne More time")
print('Run 2 result --->' + spongebob("a b cd"))


# outputs
Begin of loop, i is True, char is A and the new quote is 
Begin of loop, i is False, char is   and the new quote is A
Begin of loop, i is False, char is B and the new quote is A 
Begin of loop, i is True, char is   and the new quote is A b
Begin of loop, i is True, char is C and the new quote is A b 
Begin of loop, i is False, char is D and the new quote is A b C

After Loop, i is True and the new quote is A b Cd

Run 1 result --->A b Cd

One More time
Begin of loop, i is True, char is a and the new quote is 
Begin of loop, i is False, char is   and the new quote is A
Begin of loop, i is False, char is b and the new quote is A 
Begin of loop, i is True, char is   and the new quote is A b
Begin of loop, i is True, char is c and the new quote is A b 
Begin of loop, i is False, char is d and the new quote is A b C

After Loop, i is True and the new quote is A b Cd

Run 2 result --->A b Cd

You really must be a St. Thank you. This completely helps me understand.