Python String title() Method

The title() method returns a new string where the first character of each word is capitalized, and the rest are lowercase.

Syntax

string.title()

Parameters

No parameters

Return Value

Returns a new string where the first character of each word is capitalized and all other characters are lowercase. The original string remains unchanged.

Examples

Basic Usage

text = "hello world"
print(text.title()) # Output: Hello World

Limitations

Apostrophe

The title() method treats an apostrophe as a separator. This means that the letter immediately following an apostrophe will be capitalized.

For example:

text = "they're coming today"
print(text.title()) # Output: They'Re Coming Today

To solve this issue, you can use the capwords() function of the string module.

import string

text = "they're coming today"
print(string.capwords(text)) # Output: They're Coming Today

Numbers and Symbols

Numbers and symbols are also treated as separators, capitalizing the next alphabetical character.

text = "He came in 3rd in the chess tournament"

print(text.title()) # He Came In 3Rd In The Chess Tournament

Similarly, you can fix this issue using the capwords() function of the string module.

import string

text = "He came in 3rd in the chess tournament"

print(string.capwords(text)) # He Came In 3rd In The Chess Tournament