Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Capitalize Each Word

Strings · Python example

Capitalize Each Word

Transform a sentence with the title() string method.

Open the compilerAll examples
beginnerstringstitle casemethods

Python strings come with many built-in methods. title() capitalizes the first letter of each word and lowercases the rest.

example.py
phrase = "python makes text processing fun"
title_case = phrase.title()

print("Original:", phrase)
print("Title case:", title_case)

How the code works

  • The method scans the phrase for word starts.
  • Each word is capitalized and the remaining letters are lowercased.
  • The result is stored in title_case and printed.

Key ideas

  • Strings are immutable; methods return a new string.
  • title() treats punctuation and spaces as word boundaries.
  • The original phrase is unchanged.

Try changing it

  • Use capitalize() to change only the first character.
  • Use a list comprehension with word.capitalize() for custom rules.
  • Compare title() with string.capwords() from the string module.

Related examples

beginner

Count Vowels

Count the vowels in a sentence with a loop and a membership test.

beginner

Variables and Data Types

Create strings, integers, floats, and booleans, then inspect each type.

Previous exampleCount VowelsNext exampleList Statistics

Expected output

Original: python makes text processing fun
Title case: Python Makes Text Processing Fun

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects