šŸ˜‚ I Thought .upper() Would Fix It.

Michelle WayuaMichelle Wayua
2 min read

Problem Statement

You're given a string containing a person's name in all lowercase letters. Your task is to write a Python script that correctly formats the name so that each part starts with an uppercase letter and the rest of the letters are lowercase.

Example: Given ā€œmichelle muindiā€, your output should be ā€œMichelle Muindiā€œ.

So there I was, staring confidently at my screen thinking:

name = 'michelle muindi'
print(name.upper())

Boom!

Output:

MICHELLE MUINDI

Mission accomplished? Not quite.

I didn’t want to shout the name, just say it politely, like a well-mannered human. Turns out, there's a right way to capitalize names in Python. And yes, it’s not .upper(). Let’s walk through the correct methods step by step.

The Solution Code

def capitalized():
    uncapitalized_fullname = "michelle muindi"

    capitalized_fullname = uncapitalized_fullname.title()
    print(capitalized_fullname)

capitalized()

Deep Dive: Breaking Down Each Line of the Code.

def capitalized():

This line defines a function named ā€˜capitalizedā€˜. The parentheses () indicate it takes no parameters and the colon : marks the start of the function’s body.

    uncapitalized_fullname = "michelle muindi"

This line creates a variable named ā€˜uncapitalized_fullnameā€˜ and assigned the string ā€œmichelle muindiā€œ.

capitalized_fullname = uncapitalized_fullname.title()

This line creates another variable named ā€˜capitalized_fullnameā€˜. It calls the .title() method on the string stored in ā€˜uncapitalized_fullnameā€˜.

The .title() method converts the first letter of each word to uppercase and the rest to lowercase. So ā€˜michelle mundiā€˜ becomes ā€˜Michelle Muindiā€˜.

print(capitalized_fullname)

This line prints the value of capitalized_fullname to the console.

capitalized()

This line calls (executes) the function. When called, it runs all the steps inside the function and prints the capitalized name.

But, why bother with anything else when .capitalize() is right there?

What would happen if we used .capitalize() ? Lets see:

def capitalized():
    uncapitalized_fullname = "michelle muindi"

    capitalized_name = uncapitalized_fullname.capitalize()
    print(capitalized_name)


capitalized()

Output:

Michelle muindi

Why we didn’t use .capitalize() you ask, well, because giving love to just the first name is so last season šŸ˜‚.

The capitalize() method converts the first character of a string to uppercase and the rest of the characters to lowercase. If the first character is already uppercase or is not a letter, it remains unchanged.

šŸŽÆ Final Takeaway

When it comes to name formatting in Python, .title() is your best friend. .capitalize() is still useful, but only when working with single words.

And remember: .upper() is great when you’re shouting. Not when you’re introducing yourself at a job interview.

0
Subscribe to my newsletter

Read articles from Michelle Wayua directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Michelle Wayua
Michelle Wayua