In this post, we will discuss string methods that identify whether a string contains a space, whether it is an identifier and whether it has title cased string in it. These methods are:
-
isspace()
-
istitle()
-
isidentifier()
These methods are useful in building applications that require extensive string manipulation. Without further delay, let us dive into the details!
1. The isspace
method
The isspace
built-in method is used to check whether the string in question contains only whitespace characters or not. If the string contains only whitespace characters, it returns True and returns False otherwise. An empty string is considered to have no characters or whitespace, hence this method with return False.
Syntax of isspace
method:
string.isspace()
It doesn't take any parameters and passing a parameter gives a TypeError
. Passing the below-specified characters will return True using the isspace()
method.
'\n' – Newline character
'\v' – Vertical tab
' ' – White space
'\t' – Horizontal tab
'\f' – Feed
'\r' – Carriage return
In the example below, we have checked one of the above characters, which returns True. Try the other characters on your own and see how things execute.
Time for an example:
string = 'StudyTonight'
print(string.isspace())
string = '\n \n \n'
print(string.isspace())
string = 'Study\n Tonight'
print( string.isspace())
# Two single quotes
string = ''
print(string.isspace())
string = ' '
print(string.isspace())
print(string.isspace(' '))
Output:
False
True
False
False
True
Traceback (most recent call last):
print(string.isspace(' '))
TypeError: isspace() takes no arguments (1 given)
2. The istitle
method
The istitle
built-in Python method is used to check if the characters in a string are title-cased. Title cased refers to characters in a string wherein the first letter of every word in a string is upper case and all the other characters in that string are lower case characters.
Syntax of istitle
method:
string.istitle()
It doesn't take any parameters and returns TypeError
if a parameter is passed to it. It returns True if the string is title-cased and False otherwise.
Time for an example:
string = 'Studytonight'
print(string.istitle())
string = 'Study Tonight'
print(string.istitle())
string = 'STudy tonight'
print(string.istitle())
string = 'Studytonight Contains 12 Characters '
print(string.istitle())
print(string.istitle('Studytonight'))
Output:
True
True
False
True
Traceback (most recent call last):
print(string.istitle('Studytonight'))
TypeError: istitle() takes no arguments (1 given)
3. The isidentifier
method
As the name suggests, identifiers are used to give an identity to the variables, methods, classes and other entities that are used in building applications. There are certain rules to define an identifier. They are:
-
It can contain lower case alphabets (a-z).
-
It can contain upper case alphabets (A-Z).
-
It can contain digits (0-9), but can't begin with a digit.
-
It can contain an underscore (_), but can't begin with an underscore.
-
Special characters like !, @, #, $, % can't be used as identifiers.
-
It can be of any length.
This is a built-in method that helps identify if a string is a valid identifier or not. If an empty string is passed to this method, it returns False.
Syntax of isidentifier
method:
string.isidentifier()
It doesn't take any parameters. The string refers to the string being checked for validity. This method returns a True if the string is a valid identifier and False otherwise.
Time for an example:
string = "StudyTonight"
print(string.isidentifier())
# Identifier that contains spaces
string = "Study Tonight"
print(string.isidentifier())
# An enpty string returns False when checked for being an identifier
string = ""
print(string.isidentifier())
# Alphanumeric data
string = "12StudyTonight"
print(string.isidentifier())
# Identifier that contains underscore
string = "Study_Tonight"
print(string.isidentifier())
# Identifier that begins with an underscore
string = "_Study_Tonight"
print(string.isidentifier())
Output:
True
False
False
False
True
True
Conclusion:
In this post, we saw string methods that help in identifying spaces, title case, and identifiers that could be present in a string. Work with the code, give different input strings and observe how the output changes.
More:
Python String: isdigit(), isnumeric() and isdecimel()
Python String: islower(), isupper() and isprintable()
Python String: isalpha(), isalnum() and isascii()
Python String: split(), join() and replace()
You may also like: