| by Arround The Web | No comments

Python String Istitle() Method

To detect whether a given string is title-cased or not, use the “python string istitle()” function. The result is “true” if the first character of a string is in uppercase and the rest of the characters are in lowercase. However, the result is “false” if all the characters are in lowercase. If the string contains numerous words, make sure that the initial character of every term is in capital letters and the others are in small letters. The numbers and symbols included in a string are disregarded by this method.

Syntax of the Python Istitle() Method:

 

Example 1: Using the String Istitle() Method in Python

In this example, the “istitle()” method for strings is used in the Python code. When this method is used on a string, it returns “true” if the string’s first letter is in upper case and the other letters are in lower case. It returns “false” if all of the characters are in lower case or all of the characters are in upper case. Let’s start the code now.

We initialize the variable “string1” and set the string “California is in America” before implementing the function. This string is composed of four words. The first word is “California” which has the first letter in upper case and the rest in lower case. The second word is “is” which has both letters in lower case. The third word is “in” which also has both letters in lower case. The last word is “America” which has the first letter in upper case and the rest in lower case. The print() function is called in the line after that. And since the value is stored in the variable “string1”, we use the “istitle()” method with it.

This “istitle()” method examines each string value and returns true if all of the value’s initial characters are capitalized and all of the remaining characters are in lowercase. Otherwise, it returns false. To determine whether the script answered true or false, let’s look at the following output:


The word “is” and “in” both have lower case letters.  Their beginning characters are not in upper case so the “istitle()” method returns “false” as a result. These words in the string have some characters in block capitals and others in lowercase letters.


Then, another string value “Assembly Language Is A Low-Level Programming Language” is used in the second section of the code. We store this value in the variable “string2” which we initialize first. The string value must be written using the inverted commas. This string value has eight words, each of which begins with an upper case letter and ends with a lower case letter. Then, we use the print() function in the following line. Within this function, we use the “String2.istitle()” to determine whether the result is true or false.


As can be seen in the following figure, the output result displays true since every word in the string value began with an uppercase letter and had lowercase letters for the remainder of the word.


The istitle() method in Python just looks at the characters in a string. It ignores the numbers or symbols present in a given string. In the third part of this code, we are giving a value to a string in which we have a numeric digit. Also, the value of the string is “My Roll Number Is 161064”. This string is composed of four words and one digit number. In the next line, we use the print function. Inside of this, we use the “istitle()” method with the variable “String3” in which we store the value of the string.


As we previously mentioned, the “istitle()” function ignores any numbers that may be present in a string and only checks the string characters to determine whether the words have their first letter capitalized and whether all other letters are in lower case. As you can see in the previous image, each word has its first letter capitalized and all the other letters are in lower case. So, the function returns “True” in the output display.


This function also ignores the symbol. So, if we have symbols in the string value, it also ignores that as it ignores the numeric digit in the previous part. In this part, we have the string value “#Alex Got 96% In His Result”. This value is stored in the variable “string4” and the next line, same as in the previous example where we used the “istitle()” method inside the print() function.


Since the word’s first letters are capitalized and the symbols that are used here, the “istitle()” method is ignored and the result displays “true”.

Example 2: Utilizing the String Istitle() Method in an “If-Else” Conditional Program

In this example, we utilize the string “istitle()” function with the “If-Else” conditions. The “If” statement is printed if the criterion is fulfilled. Otherwise, the “Else” statement is shown. Let’s start writing the second example. We store the string value “I Love My Country” in the initialized variable “string” in this example. Then, in the next line, we use the “If” condition to display the if statement “This string is Titledcased” and the else statement “This string is not Titledcased” depending on the outcome of the “string.istitle()” function.


The condition is “true” and the “if” statement outputs “This string is Titledcased” as can be seen in the following figure since the string value contains four words, all of which have upper case initial characters and lower case remaining characters. The condition would be false and an else statement would be displayed if the value contained even a single word with all lowercase letters.

Example 3: Utilizing the Istitle() Method to Check for Two Strings Using the “AND” Operator in the If-Else Condition

In this example, we’re using two strings with “and” in between them and implementing the “if-else” conditions with the “istitle()” method. We have two strings in this script: “I Love Programming” is the first-string value, which is kept in the variable “string1” and “Python Is The Fastest Growing Programming Language” is the second string, which is stored in the variable “string2”. The following line applies an “if” condition.

Within the “if” condition, we use the “istitle()” function with the strings “string1” and “string2” and place the “AND” operator in between them. This method checks both strings. If they are both “title-cased”, the “if” statement “Both are title-cased” is printed. If not, the “else” statement “These don’t have title-cases” is printed. If even just one word in either of these strings does not begin with a capital letter, the string will not be “title-cased” and the “else” statement is printed as a result.


The else statement “Both are titlecased” is printed because the condition is true and both strings are titlecased.

Conclusion

In this article, we covered the Python string istitle() method to determine whether the provided string is title-cased or not. In Python programming, we used this approach in a variety of ways. At first, we displayed “true” or “false” in the output. In the second way, we applied the “istitle()” function inside of an if-else condition. Last but not the least, we used this method to check for two strings using the “AND” operator.

Share Button

Source: linuxhint.com

Leave a Reply