Methods and Functions

Python String title()

Python includes a wide range of string handling functions. String handling methods include converting a string to lowercase, uppercase, and so on. Another such method is the title case method. Python title() function is used to convert an input string to a title case i.e. converting the first alphabet of each word to uppercase and the rest into lowercase. Let us understand about Python title() in the article below.

Python title() function

Definition

  • The Python title() function is used to change the initial character in each word to Uppercase and the subsequent characters to Lowercase and then returns a new string.
  • Python title() method returns a title-cased string by converting the initial letter of each word to a capital letter.

title() Syntax

Python title() function follows the below-mentioned syntax:

                    

string.title()

title() Parameters

This function does not accept any parameter. If any parameter is passed, the function throws an exception.

Return value from title()

The title() string function returns a new string that contains a title-cased version of the input string. In Layman terms, this function capitalizes the first letter of each word in the string.

Note – Python title() function ignore any non-alphabetic characters. It ignores any numbers and special characters in the string.

Example 1: How Python title() works?

Example

                    

# Python program to illustrate title()
my_str = 'Hello, welcome to the museum'
print(my_str.title())

my_str = '101 hacks for python'
print(my_str.title())

my_str = 'I AM an MBA STUdent'
print(my_str.title())

my_str = '262131 @%&*'
print(my_str.title())

Output

                    

Hello, Welcome To The Museum
101 Hacks For Python
I Am An Mba Student
262131 @%&*

Example 2: title() with apostrophes

This function utilizes a straightforward, language-independent definition of a word as groups of consecutive letters. As a result, apostrophes (‘) are treated as a word boundary.

When using apostrophes and numbers in between words, the Python title() method may show unexpected and strange behavior. The initial letter following any number or special character (such as an apostrophe) is changed to an upper-case letter. Look at the below example to understand more:

Example

                    

# Python program to illustrate title()
my_str = "He's here, isn't he? "
print(my_str.title())

my_str = 'Pyt0n Progr6mm1ng'
print(my_str.title())

Output

                    

He'S Here, Isn'T He?
Pyt0N Progr6Mm1Ng

We don’t want this to happen most of the time. You may use regex or the string.capwords() to fix this problem.

Example 3: Using Regex or string.capwords() to Title Case String

Example

                    

# Using regex
import re

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

txt = "They're Jimmy's friends. Let's give them 10dollars each"
print(titlecasefunct(txt))
print('')

# Using capwords()
import string

s = "I'll be ca11ing you, won't I?"
val = string.capwords(s)
print(val)

Output

                    

They're Jimmy's Friends. Let's Give Them 10Dollars Each

I'll Be Ca11ing You, Won't I?

Frequently Asked Questions

Q1. What is title() in Python?

A title-cased string is the one in which every first letter of the word is an uppercase character. Python title() function is used to convert an input string to a title case i.e. converting the first alphabet of each word to uppercase and the rest into lowercase. Python title() function ignore any non-alphabetic characters. It ignores any numbers and special characters in the string.

Q2. How do you add a title in Python?

To add a title to an input string in Python, we implement the title() string class method. Python title() function follows the below-mentioned syntax:

                    

string.title()

The title() string function returns a new string that contains a title-cased version of the input string. In Layman terms, this function capitalizes the first letter of each word in the string.

Example

                    

txt = 'i have to reach office by 9:00'
print(txt.title())

txt = 'HELLO Students, WELcome BAck'
print(txt.title())

txt = '101hacks with python'
print(txt.title())

Output

                    

I Have To Reach Office By 9:00
Hello Students, Welcome Back
101Hacks With Python

Q3. What is the difference between title and capitalize in Python?

Python capitalize() and title() are 2 inbuilt functions used for string handling.

  • title() – Python title() method returns a title cased string by converting the initial letter of each word to a capital letter.
  • capitalize() – The capitalize() method only capitalizes the first character of the string i.e. it converts only the first letter of the input string into uppercase and others to lowercase.

Example

                    

txt = 'i have to reach office by 9:00'
print(txt.title())
print(txt.capitalize())
print('')

txt = 'HELLO Students, WELcome BAck'
print(txt.title())
print(txt.capitalize())
print('')

txt = '101hacks with python'
print(txt.title())
print(txt.capitalize())

Output

                    

I Have To Reach Office By 9:00
I have to reach office by 9:00

Hello Students, Welcome Back
Hello students, welcome back

101Hacks With Python
101hacks with python

Q4. Difference between istitle() and title() function in Python?

  • Python title() – The Python title() function is used to change the initial character in each word to Uppercase and the subsequent characters to Lowercase and then returns a new string.
  • Python istitle() – Python istitle() function is used to return value TRUE if the input string is a titlecased string else it returns FALSE. The istitle() method disregards any symbols or digits in a provided string.

Example

                    

txt = 'i have to reach office by 9:00'
print(txt.title())
print(txt.istitle())
print('')

txt = 'Hello Students, Welcome Back'
print(txt.title())
print(txt.istitle())
print('')

txt = '101hacks with python'
print(txt.title())
print(txt.istitle())

Output

                    

I Have To Reach Office By 9:00
False

Hello Students, Welcome Back
True

101Hacks With Python
False

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.