Yandex

Python String title() Method – Convert to Title Case


Python String title() Method

The title() method in Python returns a copy of the string where each word starts with an uppercase letter and the remaining letters are lowercase. It’s useful for formatting titles, names, or headings.

Syntax

string.title()

Parameters:

None – this method does not take any arguments.

Returns:

A new string with the first letter of each word in uppercase and all other characters in lowercase.

Example: Basic Usage

text = "python is fun"
title_text = text.title()
print(title_text)
Python Is Fun

Example: Mixed Case Input

text = "wElcOme to PYTHON world!"
print(text.title())
Welcome To Python World!

Use Case: Formatting User Input

If you're building a form or app where users input names, you can use title() to standardize formatting:

name = input("Enter your full name: ")
print("Formatted Name:", name.title())

Limitations

  • title() capitalizes letters after any non-letter character, including numbers and punctuation.
  • It may not correctly handle acronyms or special cases (like “McDonald”).

Example: Words with Apostrophes or Special Characters

text = "they're learning python's power!"
print(text.title())
They'Re Learning Python'S Power!

This behavior might not be desirable in every case, so use title() carefully.

When to Use title()

  • To format headings or titles
  • To display names in proper case
  • To clean up text input from users

Common Mistake

Thinking title() changes the original string. It does not. It returns a new string:

text = "hello world"
text.title()  # This won't change text
print(text)      # Output: hello world

Interview Tip

Know the difference between title() and capitalize(). title() capitalizes the first letter of each word, while capitalize() only affects the first letter of the entire string.

Summary

  • title() returns a new string in title case
  • Does not modify the original string
  • Useful for formatting names and titles
  • Be cautious with punctuation and contractions

Practice Problem

Ask the user for a book title and print it in proper title case:

book = input("Enter book title: ")
print("Formatted:", book.title())


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M