Title Function in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The title() function of str class in Python is used to create a new string from an existing string by converting it to a title which means the first letter of each word is capitalized, and all remaining characters become lower case. It ignores any non-alphabetic characters.

Syntax of title() in Python

The syntax of the title function in Python is:

where string is the name of the string.

Parameters of title() in Python

The title method in Python doesn't take any parameters.

Return Values of title() in Python

Return Type: str

title in Python returns a string with the first character of each word capitalized (if the first character is a letter) and the remaining characters as lowercase.

Examples of title() in Python

Output

What is title() in Python?

Python's str class provides several methods to perform some common operations on a string.

One such method is title. The title method is used to convert a string to a title. In a title, the first character of every word is capitalized, and the rest of the letters are in lowercase. If a letter except the first one is already in lowercase, then it remains unchanged.

Note:

  • Python title() function ignore any non-alphabetic characters. It ignores any numbers and special characters in the string.
  • The title function does not modify the original string. It only returns a new string.

More Examples

Example 1: title() ignores non-alphabetic characters

Output:

Explanation:

The title function ignores any number or special character in the string. In this example, 10 and !! remain unchanged, while alphabetic characters are changed according to their position in the string. The first letter becomes uppercase, and the remaining lowercase.

Example 2: Real-world example of using title() in Python

Output:

Explanation:

Suppose you want to take a full name as input from the user and make sure it is in title form. Now, instead of forcing the user to enter the name in the correct format, you can take the input and transform it into the correct title format yourself using the title() method of str class.

Example 3: title() with apostrophes

Output:

Explanation:

The title function in Python also capitalizes the first letter after the apostrophes.
In this example, She's is transformed to She'S and isn't is transformed to isn'T. This can be an issue sometimes. We can solve it with regex, as explained in the next example.

A Regular Expression (RegEx) is a sequence of characters that defines a search pattern.

Example 4: Using Regex to Title Case String

Output:

Explanation:

We are using Python's in-built regex module re to create a user-defined function for title casing a string. The function takes a regular expression pattern and replaces the pattern occurrence using the lambda function provided as the second argument. The regular expression used here, [A-Za-z]+('[A-Za-z]+)? matches with all words starting with an alphabet or all words that have alphabets after an apostrophe '.

Here's a detailed explanation:

Regex tokenWhat it does
[]Match Any character in the set
A-ZMatches a character in the range "A" to "Z"
a-zMatches a character in range "a" to "z"
+Match 1 or more of the preceding token
()Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
'Matches an apostrophe character
?Matches 0 or 1 of the preceding token, effectively making it optional.

This solved the issue of a letter getting capitalized after the apostrophe.

Conclusion

  • The str class provides the title function.
  • The title function converts a string into a title by uppercasing the first character and lowercasing the remaining characters.
  • It doesn't take any parameters and returns a string.
  • Python title() function ignore any non-alphabetic characters.

See Also: