If you have been following this series, you might be well aware that we have discussed various string methods like isalpha(), isalnum() and isascii() in our previous post. In this article, we will discuss the below string methods.
-
islower
method
-
isupper
method
-
isprintable
method
These methods come in handy while manipulating and handling strings for Python applications.
1. The islower
method
The islower
is a built-in method in Python that checks if a string contains all lower case characters or not. It returns True if all the alphabet present in the string are in lower case even if there are digits or symbols present in the string. It returns False if the string is empty or it contains upper case characters/alphabets.
Syntax of islower
method
string.islower()
This method takes no parameters. If any parameters are passed, it gives a TypeError
. It checks if all the characters in the string are in lower case. The string here refers to the string that is being checked for having only lower case characters. Let's have a code example to see how this method works:
string = "StudyTonight"
print(string.islower())
string = 'studytonight1@23'
print(string.islower())
string = 'STUDYTONIGHT'
print(string.islower())
string = 'studytonight'
print(string.islower())
string = ' '
print(string.islower())
# this will lead to error
print(string.islower('ABC'))
Output:
False
True
False
True
False
Traceback (most recent call last):
print(string.islower('ABC'))
TypeError: islower() takes no arguments (1 given)
2. The isupper
method
The isupper
is a built-in method in Python that checks if a string contains all upper case characters or not. It returns True if all the alphabet present in the string are in upper case even if there are digits or symbols present in the string. It returns False if the string is empty or it contains lower case characters.
Syntax of isupper
method
string.isupper()
It takes no parameters and gives a TypeError
in case parameters are passed. Let's take an example to cover a few use cases,
string = "StudyTonight"
print(string.isupper())
string = 'STUDYTONIGHT1@23'
print(string.isupper())
string = 'STUDYTONIGHT'
print(string.isupper())
string = 'studytonight'
print(string.isupper())
string = ' '
print(string.isupper())
# will lead to error
print(string.isupper('ABC'))
Output:
False
True
True
False
False
Traceback (most recent call last):
print(string.isupper('ABC'))
TypeError: isupper() takes no arguments (1 given)
3. The isprintable
method
The isprintable
is a built-in Python method which checks if the calling string is printable or not printable. If the string in question is printable or empty, it returns True. It returns False otherwise.
The printable characters here refers to digits (0-9), alphabets (lower case: a-z and upper case: A-Z), space, and special characters (%&'()*+, -./!”#$:;?| @[\]^_`{}~)
Syntax of isprintable
method
string.isprintable()
This method takes no parameters and returns True or False depending on whether the characters in the string are printable or not.
If a parameter is passed to the isprintable
method, it gives a TypeError
. If whitespace or multiple whitespaces are checked for being a printable string, it gives True. An empty string (' ' or " ", i.e two single quotes or two double quotes) also returns True when checked for being printable. Let's take an example,
string = 'The website is StudyTonight'
print(string.isprintable())
string = 'The website is StudyTonight!'
print(string.isprintable())
string = 'The website is \n Studytonight !'
print(string.isprintable())
string = ""
print( string.isprintable())
string = ''
print( string.isprintable())
string = ' '
print( string.isprintable())
# this will lead to error
print(string.isprintable('Studytonight'))
Output:
True
True
False
True
True
True
Traceback (most recent call last):
print(string.isprintable('Studytonight'))
TypeError: isprintable() takes no arguments (1 given)
Conclusion
In today's post, we saw how lower case and upper case characters in a string can be identified, and how printable strings can be identified.
Don't forget to execute the code demonstrated and play around with it to see how things work.
You may also like: