Python Reference Library →

Python String capitalize() – Capitalize First Letter of a String

Let’s look at string capitalize() – capitalize first letter of a string

  1. Notice

    capitalize() returns a string with its first character titlecased and the remaining characters lowercased.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'hELLO WORLD'
result = text.capitalize()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'hELLO WORLD'Object 2 · str'Hello world'
This step

capitalize() returns a string with its first character titlecased and the remaining characters lowercased.

Picture string capitalize() – capitalize first letter of a string

capitalize() returns a string with its first character titlecased and the remaining characters lowercased.

Image brief

Alt text
A diagram for Python String capitalize() – Capitalize First Letter of a String. Instructions: text = 'hELLO WORLD' result = text.capitalize() print(result). Displayed result: Hello world. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'hELLO WORLD'; object 2: 'Hello world'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String capitalize() – Capitalize First Letter of a String". Teaching focus: capitalize() returns a string with its first character titlecased and the remaining characters lowercased. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'hELLO WORLD' result = text.capitalize() print(result) | v DISPLAYED RESULT Hello world FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: 'hELLO WORLD' object 2: 'Hello world' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String capitalize() – Capitalize First Letter of a String. Instructions: text = 'hELLO WORLD' result = text.capitalize() print(result). Displayed result: Hello world. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'hELLO WORLD'; object 2: 'Hello world'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'hELLO WORLD'
result = text.capitalize()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'hELLO WORLD'Object 2 · str'Hello world'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
Hello world

What do you expect this program to print?

What does this example display?

text = 'hELLO WORLD'
result = text.capitalize()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

Hello world

Follow the printed values in order. The output is: Hello world

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

capitalize() returns a string with its first character titlecased and the remaining characters lowercased.

capitalize() returns a string with its first character titlecased and the remaining characters lowercased.

More examples and reference

Python String capitalize() Method

The capitalize() method in Python is used to convert the first character of a string to uppercase and the rest to lowercase.

This method is useful when you want to format strings to look like proper sentences or names.

Syntax

string.capitalize()

Returns:

  • A new string with the first character capitalized and the rest in lowercase.

Example 1: Basic Usage

text = "hello world"
print(text.capitalize())
Hello world

Example 2: All Caps to Capitalized

text = "PYTHON IS FUN"
print(text.capitalize())
Python is fun

Example 3: Already Capitalized

text = "Python programming"
print(text.capitalize())
Python programming

Example 4: Starting with Non-letter Character

text = "123hello"
print(text.capitalize())
123hello

Note: Only the first letter is affected. If the first character is not a letter, nothing changes.

Use Cases

  • Formatting user input (e.g., names, titles)
  • Creating proper sentence case for display
  • Ensuring consistency in strings for comparisons

Does it Modify the Original String?

No. Strings in Python are immutable. capitalize() returns a new string and does not change the original one.

Common Mistakes

  • Assuming it capitalizes every word – it only affects the first character.
  • For capitalizing every word, use title() instead.

Interview Tip

capitalize() is often used in string normalization or preprocessing tasks before comparison or display.

Summary

  • capitalize() returns a new string with the first character uppercase and the rest lowercase.
  • Original string remains unchanged.
  • Non-letter first characters are not affected.

Practice Problem

Write a program that takes a sentence as input and prints the capitalized version.

sentence = input("Enter a sentence: ")
print("Capitalized:", sentence.capitalize())

What to remember

Connect what you noticed in the example with the idea below.

Remember

capitalize() returns a string with its first character titlecased and the remaining characters lowercased.

Watch out for this idea

capitalize() makes the first letter of every word uppercase.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →