Search for Courses, Topics
certificate icon
Certificate

Learn on Scaler Topics and get certified.

static-certificate

Title Function in Python

Learn about title() function in python.

22 Feb 2022-4 mins read

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:

string.title()

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.

Example of title() in Python

my_string = "follow SCALER topics on twitter"

my_title = my_string.title()

print(my_title)

Output

Follow Scaler Topics On Twitter

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.

Examples

Example 1: title() ignores non-alphabetic characters

my_string = "10 best e-Commerce webSites!!"

my_title = my_string.title()

print(my_title)

Output:

10 Best E-Commerce Websites!!

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

full_name = input("Enter Full Name: ")

full_name_fixed = full_name.title()

print(full_name_fixed)

Output:

Enter Full Name: yuvraj singh jadon
Yuvraj Singh Jadon

Explanation:

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

Example 3: title() with apostrophes

my_string = "She's out of town, isn't she?"

print(my_string.title())

Output:

She'S Out Of Town, Isn'T She?

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, 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

import re

def my_title(string):
    return re.sub(
              r"[A-Za-z]+('[A-Za-z]+)?",
              lambda ch: ch.group(0)[0].upper() +
              ch.group(0)[1:].lower(),
              string
    )

text = "She's out of town, isn't she?"
print(my_title(text))

Output:

She's Out Of Town, Isn't She?

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 apostrophes '.

Here's a detailed explaination:

Regex tokenWhat it does
[]Match Any character in the set
A-ZMatches a character in 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 title function is provided by the str class.
  • The title function is used to convert 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.