Methods and Functions

Python String istitle()

You might have heard about the Python istitle() function. What exactly do we use it for? Before understanding the operations of istitle(), let us first understand what are title-cased strings. A title-cased string is the one in which every first letter of the word is an uppercase character. Python istitle() string class method tells us whether the input string is a title-cased string or not.

Python istitle() function

Definition

  • Python istitle() function is used to return value TRUE if the input string is a titlecased string else it returns FALSE.
  • The Python istitle() string method checks whether each word’s first character is uppercase and the rest are in lowercase or not.

Python istitle()

Python istitle() is a built-in string handling function in Python. When this function is used on a string, it returns True if the first character of the string is in uppercase and the rest are in lowercase. If a string contains multiple words, the initial letter should be uppercase and the remaining characters should be lowercase.

For example, ‘I Love Python Programming’ is a titlecased string, but ‘I love Python programming’ is not a titlecased string.

The istitle() method disregards any symbols or digits in a provided string.

istitle() Syntax

Python istitle() uses the following syntax:

                    

string.istitle()

istitle() Parameters

The Python istitle() string class function does not accept any parameters. If any parameter is passed, then the function throws an error.

Return value from istitle()

Python istitle() returns the following Boolean values:

  • True – if the input string is a titlecased string
  • False – if the input string is not a titlecased string or if the string is empty

Note – This function ignores any numbers and symbols specified in the string.

Example 1: Working of istitle()

Example

                    

# Python program to illustrate istitle()
# titlecased string
txt = 'Good Morning Everyone'
print(txt.istitle())   # returns True

# only 1 titlecase
txt = 'I love to eat icecream'
print(txt.istitle())   # returns False

# all letters are uppercase
txt = 'HELLO'
print(txt.istitle())   # returns False

# string contains more than 1 uppercase in one single word
txt = 'PythonTutorial'
print(txt.istitle())   # returns False

# string with numerical
txt = 'This string contains number 100'
print(txt.istitle())   # returns False

# titlecased numeric string
txt = '100 Number Is Lucky'
print(txt.istitle())   # returns True

# titlecased string with symbol and number
txt = 'Glad To See You @*123'
print(txt.istitle())   # returns True

Output

                    

True
False
False
False
False
True
True

Example 2: Working with istitle()

Example

                    

# Python program to illustrate istitle()
# empty string
txt = ''
print(txt.istitle())   # returns False

# only a numeric value
txt = '25'
print(txt.istitle())   # returns False

# single symbol
txt = '$'
print(txt.istitle())   # returns False

txt = 'Ā Łuxury Çar'
print(txt.istitle())   # returns True

Output

                    

False
False
False
True

An empty string, a numeric string, or a string containing just symbols are not titlecased.

Example 3: How to use istitle()?

The istitle() function can be used in real-life applications such as:

                    

s = 'I Am An Engineering Graduate'
if(s.istitle()) == True:
    print('String is Titlecased')
else:
    print('String is not Titlecased')

s = 'I love to Watch movies'
if(s.istitle()) == True:
    print('String is Titlecased')
else:
    print('String is not Titlecased')

Output

                    

String is Titlecased
String is not Titlecased

Frequently Asked Questions

Q1. What does istitle mean in Python?

A title-cased string is the one in which every first letter of the word is an uppercase character. Python istitle() string class method tells us whether the input string is a title-cased string or not. Python istitle() function is used to return value TRUE if the input string is a titlecased string else it returns FALSE.

If a string contains multiple words, the initial letter should be uppercase and the remaining characters should be lowercase. For example, ‘I Love Python Programming’ is a titlecased string, but ‘I love Python programming’ is not a titlecased string.

The istitle() method disregards any symbols or digits in a provided string.

Q2. What does ljust mean in Python?

In Python, ljust means to align an input string to the left of a given minimum width. To left align a text in Python, we use the Python’s string ljust() function. The string ljust() method returns a left-justified string with a minimum width specified. The syntax for Python ljust() function is as follows:

                    

string.ljust(width, fillchar)

If the fillchar argument in the syntax of the function is specified, the defined character is also used to fill the remaining space.

Example

                    

# without fillchar parameter
txt = 'Countries'
new_str = txt.ljust(15)
print('Left-justified string:', new_str)

# with fillchar parameter
new_str = txt.ljust(15, '*')
print('Left-justified string:', new_str)

Output

                    

Left-justified string: Countries
Left-justified string: Countries******

Q3. How do you justify in Python?

Python provides us with 3 different built-in functions that helps us to justify the input string. These 3 functions are as follows:

  • center() – The Python center() method is an inbuilt function that is used to align a string to the center by filling paddings to the left and right of the string.
  • ljust() – The Python string ljust() method returns a left-justified string with a minimum width specified.
  • rjust() – The string rjust() method returns a right-justified string with a minimum width specified.

Example

                    

mytxt = 'Python'

# Center-justified string
j = mytxt.center(16, '*')
print('Centered string:', j)

# Left-justified string
j = mytxt.ljust(16, '*')
print('Left-justified string:', j)

# Right-justified string
j = mytxt.rjust(16, '*')
print('Right-justified string:', j)

Output

                    

Centered string: *****Python*****
Left-justified string: Python**********
Right-justified string: **********Python

Q4. How do you swapcase in Python?

The Python string swapcase() function returns the string after converting all uppercase characters to lowercase and all lowercase characters to uppercase. The syntax for the swapcase() function is as follows:

                    

string.swapcase()

Example

                    

mystr = 'PytHon ProGrammInG'
print('Original string:', mystr)
print('Swapcased string:', mystr.swapcase())

Output

                    

Original string: PytHon ProGrammInG
Swapcased string: pYThON pROgRAMMiNg

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.