This article explains how to change case to lowercase and uppercase in python.
Python has a built-in method related to string handling.
You are not only can check whether the string is lowercase or uppercase, but you also can change a string to one of these case characteristics.
In this article, we will explain four of python’s built-in methods to check and change the string.
isupper()
isupper()
is a built-in method used to check if the argument contains any uppercase characters such as ABCDEFGHIJKLMNOPQRSTUVWXYZ.
The following python code is an example of the use of isupper()
method:
str = 'UPPERCASE'
print(str.isupper())
str = 'Some UPPER'
print(str.isupper())
Output:
True
False
isupper()
method does not take any parameters. It returns True if all characters in the string are uppercase, and it returns False if the string contains one or more non-uppercase characters.
islower()
opposite to isupper()
method, islower()
method used to check if the argument contains any lowercase characters such as abcdefghijklmnopqrstuvwxyz.
The following python code is an example of the use of islower()
method:
str = 'lowercase'
print(str.islower())
str = 'Some lower'
print(str.islower())
Output:
True
False
islower()
method does not take any parameters. It returns True if all characters in the string are lowercase, and it returns False if the string contains one or more non-lower characters.
Both, isupper() and islower() methods will return True for whitespace, digits, and symbols
To convert a string to lowercase and uppercase, we can use lower()
and upper()
method respectively.
Lowercase and Uppercase in Python
lower()
lower()
is a built-in method used to converts all uppercase characters to lowercase. It will return the original string if there are no uppercase characters.
The following python code is an example of the use of lower()
method:
str = 'UPPERTOLOWER'
print(str.lower())
str = 'UpPer 123 ? lower'
print(str.lower())
Output:
uppertolower
upper 123 ? lower
upper()
upper()
is a built-in method used to converts all lowercase characters to uppercase. It will return the original string if there are no lowercase characters.
The following python code is an example of the use of upper()
method:
str = 'lowertoupper'
print(str.upper())
str = 'lower 123 ? UpPer'
print(str.upper())
Output:
LOWERTOUPPER
LOWER 123 ? UPPER
As shown in the example, both lower() and upper() method does not take any arguments, and it will return digits and symbols as it is.
Leave a Reply