Python String title() Method

Converts string to “Title Case”

Usage

The title() method returns a copy of the string with first letter of each word is converted to uppercase and remaining letters are lowercase.

The method does not change the original string.

Syntax

string.title()

Basic Example

Example: Convert string to titlecase

S = 'hello, world!'
x = S.title()
print(x)	# Hello, World!

Unexpected Behavior of title() Method

The first letter after every number or special character (such as Apostrophe) is converted into a upper case letter.

Example:

S = "c3po is a droid"
x = S.title()
print(x)	# C3Po Is A Droid
S = "they're bob's friends."
x = S.title()
print(x)	# They'Re Bob'S Friends.

Workaround

As a workaround for this you can use string.capwords()

Example:

import string
S = "c3po is a droid"
x = string.capwords(S)
print(x)	# C3po Is A Droid
import string
S = "they're bob's friends."
x = string.capwords(S)
print(x)	# They're Bob's Friends.