Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python String istitle() method

Python String Istitle() Method

In this article, we will unveil Python String istitle() function. Python String has got built-in functions to perform operations over the input string. Python String istitle() is one such method.

Getting started with Python String istitle() method

String istitle() method is used to check the title case of the input string i.e. it checks and returns True if only the first character of each word of the string is uppercase and all the remaining characters of each word of the string are lowercase.

Example 1:

inp = 'Taj Mahal'
print(inp.istitle()) 

In the above example, the istitle() function returns True because for every word of the above input, only the first character is uppercase.

Output:

True

Example 2:

inp = 'Taj mahal'
print(inp.istitle()) 

In this example, the istitle() method results in False because the second word of the input string i.e. ‘mahal’ doesn’t have an uppercase first character.

Output:

False

Example 3:

inp = 'TAJ MAHAL'
print(inp.istitle()) 

In this example, every character of the input string is in Uppercase. Thus the function returns False.

Output:

False

NumPy istitle() method

NumPy module has a built-in istitle() method to check for the title case of the input array.

The numpy.char.istitle() method works in an element-wise fashion. It checks the title case of every element of the array individually and returns True/False for the same.

Note: If an input string contains zero characters, by default, the function returns False.

Syntax:

numpy.char.istitle(input_array)

Example:

import numpy 


inp_arr1 = numpy.array(['TAJ', 'Mahal', '14Pen', '20eraser', 'aMAZON', 'F21Ever']) 

print ("Elements of the array:\n", inp_arr1) 

res1 = numpy.char.istitle(inp_arr1) 
print ("Array after istitle():\n", res1 ) 

Output:

Elements of the array:
 ['TAJ' 'Mahal' '14Pen' '20eraser' 'aMAZON' 'F21Ever']
Array after istitle():
 [False  True  True False False  True]

Pandas istitle() method

Pandas module consists of the Series.str.istitle() method to check for the title case of the input data.

The Series.str.istitle() method checks whether all the string in the data-set/input is title case or not in an element-wise fashion.

Syntax:

Series.str.istitle()

Example:

import pandas
res = pandas.Series(['TAJ', 'Mahal', '14Pen', '20eraser', 'aMAZON', 'F21Ever'])
print(res.str.istitle())

As seen above, the presence of digits in the input data doesn’t bring any change to the output of the function.

Output:

0    False
1    True
2    True
3    False
4    False
5    True
dtype: bool

Conclusion

In this article, we did understand the working of Python istitle() function under various scenarios.


References

Python String istitle() method

Leave a Reply

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