› Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 6: Python › MITx 6.100L Introduction to CS and Programming Using Python › Understanding Merge Sort: Divide, Conquer, and Merge
- This topic is empty.
-
AuthorPosts
-
June 22, 2026 at 7:05 am #6980
Most beginners encounter Python string methods very early in their programming journey. One of the most common tasks is changing the capitalization of text.
A frequent beginner mistake is using the wrong string method when trying to capitalize the first letter of each word.
For example, a learner might write:
text = "my name is John" uppercasedtext = text.upper() print(uppercasedtext)While this code runs successfully, it does not produce the desired result.
Instead of capitalizing the first letter of each word, it converts every letter to uppercase.
Understanding the Output
The above code produces:
MY NAME IS JOHNThis happens because the
upper()method converts all lowercase letters in a string into uppercase letters.Think of it as turning on the “Caps Lock” key for the entire string.
The Correct Method for Capitalizing Each Word
If the goal is to capitalize the first letter of every word, Python provides the
title()method.Example:
text = "my name is John" capitalized_text = text.title() print(capitalized_text)Output:
My Name Is JohnNotice that each word now begins with a capital letter.
How the title() Method Works
Consider the string:
my name is JohnThe
title()method processes each word individually:my → My name → Name is → Is John → JohnThe final result becomes:
My Name Is JohnThis makes
title()particularly useful for:- Names
- Titles
- Headings
- Formatted text output
Another Useful Method: capitalize()
Python also provides the
capitalize()method.Example:
text = "my name is john" print(text.capitalize())Output:
My name is johnNotice the difference.
Only the first letter of the entire string becomes uppercase.
The remaining letters are converted to lowercase.
Comparing the Three Methods
Suppose we start with:
text = "my name is john"Using
upper():print(text.upper())Output:
MY NAME IS JOHNUsing
title():print(text.title())Output:
My Name Is JohnUsing
capitalize():print(text.capitalize())Output:
My name is johnEach method serves a different purpose.
Visualizing the Difference
Original: my name is john upper(): MY NAME IS JOHN title(): My Name Is John capitalize(): My name is johnA quick visual comparison often makes the distinction much easier to remember.
When Should Each Method Be Used?
upper()— When all letters should be uppercase.title()— When every word should start with a capital letter.capitalize()— When only the first letter of the entire sentence should be capitalized.
Examples:
WARNING MESSAGE → upper() Book Title → title() Sentence formatting → capitalize()
The Correct Solution
For the original requirement:
Capitalize the first letter of each word.
The correct Python code is:
text = "my name is John" capitalized_text = text.title() print(capitalized_text)Output:
My Name Is John
Key Takeaway
Many beginners assume that
upper()means “make the first letter uppercase.” In reality, it converts every letter to uppercase.Remember:
- upper() → ALL LETTERS CAPITAL
- title() → Every Word Starts With A Capital
- capitalize() → Only The First Letter Of The Sentence Is Capitalized
Whenever the requirement is to capitalize the first letter of each word,
title()is usually the simplest and most appropriate solution. -
AuthorPosts
- You must be logged in to reply to this topic.
