Python is a versatile programming language that upholds numerous built-in string methods. Strings are basically a collection of characters and they are commonly used in many programming languages such as Python, Java, C++, etc. There are many built-in string processing methods that serve different functionalities in Python such as lower(), islower(), upper(), isupper(), etc.
In this Python guide, you will learn:
- How can the Lower() method be used to Lowercase a String
- How does the islower() Method Determine Whether a String is Lowercase
- How can the Upper() method be used to Uppercase a String
- How does the isupper() Method Determine Whether a String is Uppercase
Let’s begin with the lower() method.
How can the Lower() Method Be Used To Lowercase a String?
Using the lower() method which is Python’s built-in method, uppercase characters are changed to lowercase in the string. If an uppercase character is present, it is converted to lowercase. This method doesn’t take any argument.
For instance,
string1 = 'WELCOME TO PYTHON PROGRAMMING'
print(string1.lower())
string2 = 'Hello Python!'
print(string2.lower())
In the above code:
- The first string consists of all the characters which are uppercase. They are converted to lowercase with the help of the lower() method.
- The second string contains two characters which are uppercase. They are converted to lowercase with the use of the lower() method.
The output is as follows:
From the above snapshot, we can see that both the strings are converted to lowercase.
How does the islower() Method Determine Whether a String is Lowercase
islower() is also a built-in method in Python used for string processing. The stated method retrieves True if only lowercase characters are available/present in the string. If either one of the characters is uppercased, it returns False.
For instance,
string1 = 'john loves python'
print(string1.islower())
string2 = 'Python is Fun'
print(string2.islower())
In the above code:
- The first string named string1 consists of lowercase characters. The islower() method will test whether all the characters/alphabets in the string are lowercase, and it will retrieve True if they are.
- The second string comprises of both uppercase and lowercase characters. The islower() method is implemented to test if all the characters/alphabets are lowercase, it will return False.
The output is as follows:
It can be seen that the first string returned True because the condition is True, and the second string returned False because the condition is not True.
How can the Upper() method be used to Uppercase a String?
Similar to the lower() method, the upper() method is also a built-in method in Python used for string processing. Using the stated method, the lowercase characters/alphabets in the string are converted to uppercase. If there isn’t a lowercase string, the original string is returned. This method doesn’t take any argument.
Let’s see this method practically with the help of an example:
string1 = 'python programming'
print(string1.upper())
string2 = 'My name is Stephen'
print(string2.upper())
In the above code:
- The first string contains all the characters which are in lowercase. The upper() method is used to convert them into uppercase.
- The second string contains 2 characters which are uppercased. To lowercase them, use the lower() function.
The output of the above code is as follows:
From the above snapshot, we can see that both the strings are converted to uppercase.
How Does the isupper() Method Determine Whether a String is Uppercase?
The isupper() method determines whether every character in the string is capitalized and returns True if they are. If either one of the characters is lowercase, it returns False.
For instance,
string1 = 'JOHN LOVES PYTHON'
print(string1.isupper())
string2 = 'Python is Easy'
print(string2.isupper())
In the above code:
- The first string comprises all the uppercase characters. The isupper() method will check if all the characters are uppercase, which is true in this case, so it will return True.
- Characters in the second string are both uppercase and lowercase. The isupper() method will check if all the characters are uppercase which is not True, so it will return False.
Conclusion
Python offers several built-in string processing methods, such as lower(), islower(), upper(), isupper(), etc. The lower() method changes the uppercase characters in the string to lowercase whereas the upper() method changes the lowercase characters in the string to uppercase. This guide has demonstrated different methods to lower and upper a string.