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
# Convert string to titlecase
S = 'hello, world!'
x = S.title()
print(x)
# Prints 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.
S = "c3po is a droid"
x = S.title()
print(x)
# Prints C3Po Is A Droid
S = "they're bob's friends."
x = S.title()
print(x)
# Prints They'Re Bob'S Friends.
Workaround
As a workaround for this you can use string.capwords()
import string
S = "c3po is a droid"
x = string.capwords(S)
print(x)
# Prints C3po Is A Droid
import string
S = "they're bob's friends."
x = string.capwords(S)
print(x)
# Prints They're Bob's Friends.