š I Thought .upper() Would Fix It.

Problem Statement
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.
Subscribe to my newsletter
Read articles from Michelle Wayua directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
