isupper(), islower(), lower(), upper() in Python and their applications
In this article, we will discuss about isupper(), islower(), upper(), and lower() functions in Python. These methods are built-in methods used for handling strings. Before studying them in detail let’s get a basic idea about the functioning of these functions.
- The functions isupper() and islower() returns the boolean True value if the all the characters of the string are in upper case or lower case respectively.
- The functions upper() and lower() returns the string by converting all the characters of the string to upper case or lower case respectively
Now let’s discuss these functions in detail.
isupper()
In Python, isupper() is a built-in method used for string handling. This method returns True if all characters in the string are uppercase, otherwise, returns “False”.
This function is used to check if the argument contains any uppercase characters such as :
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Syntax :
string.isupper() Parameters: isupper() does not take any parameters Returns :
- True- If all characters in the string are uppercase.
- False- If the string contains 1 or more non-uppercase characters.
Examples:
Input : string = 'GEEKSFORGEEKS' Output : True Input : string = 'GeeksforGeeks' Output : False
Errors And Exceptions
- It returns “True” for whitespaces but if there is only whitespace in the string then returns “False”.
- It does not take any arguments, Therefore, It returns an error if a parameter is passed.
- Digits and symbols return “True” but if the string contains only digits and numbers then returns “False”
# Python code for implementation of isupper() # checking for uppercase characters string = 'GEEKSFORGEEKS' print (string.isupper()) string = 'GeeksforGeeks' print (string.isupper()) |
Output:
True False
islower()
In Python, islower() is a built-in method used for string handling. The islower() method returns True if all characters in the string are lowercase, otherwise, returns “False”.
This function is used to check if the argument contains any lowercase characters such as :
abcdefghijklmnopqrstuvwxyz
Syntax :
string.islower() Parameters: islower() does not take any parameters Returns :
- True- If all characters in the string are lower.
- False- If the string contains 1 or more non-lowercase characters.
Examples:
Input : string = 'geeksforgeeks' Output : True Input : string = 'GeeksforGeeks' Output : False
Errors And Exceptions
- It returns “True” for whitespaces but if there is only whitespace in the string then returns “False”.
- It does not take any arguments, Therefore, It returns an error if a parameter is passed.
- Digits and symbols return “True” but if the string contains only digits and numbers then returns “False”.
# Python code for implementation of isupper() # checking for lowercase characters string = 'geeksforgeeks' print (string.islower()) string = 'GeeksforGeeks' print (string.islower()) |
Output:
True False
lower()
In Python, lower() is a built-in method used for string handling. The lower() methods returns the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string.
Syntax :
string.lower() Parameters: lower() does not take any parameters Returns : It converts the given string in into lowercase and returns the string.
Examples:
Input : string = 'GEEKSFORGEEKS' Output : geeksforgeeks Input : string = 'GeeksforGeeks' Output : geeksforgeeks
Errors And Exceptions
- It does not take any arguments, Therefore, It returns an error if a parameter is passed.
- Digits and symbols return are returned as it is, Only an uppercase letter is returned after converting to lowercase.
# Python code for implementation of lower() # Checking for lowercase characters string = 'GEEKSFORGEEKS' print (string.lower()) string = 'GeeksforGeeks' print (string.lower()) |
Output:
geeksforgeeks geeksforgeeks
upper()
In Python, upper() is a built-in method used for string handling. The upper() methods returns the uppercased string from the given string. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string.
Syntax :
string.upper() Parameters: upper() does not take any parameters Returns : It converts the given string in into uppercase and returns the string.
Examples:
Input : string = 'geeksforgeeks' Output : GEEKSFORGEEKS Input : string = 'My name is ayush' Output : MY NAME IS AYUSH
Errors And Exceptions
- It does not take any arguments, Therefore, It returns an error if a parameter is passed.
- Digits and symbols return are returned as it is, Only a lowercase letter is returned after converting to uppercase.
# Python code for implementation of upper() # checking for uppercase characters string = 'geeksforgeeks' print (string.upper()) string = 'My name is ayush' print (string.upper()) |
Output:
GEEKSFORGEEKS MY NAME IS AYUSH
Application : Given a string, the task is to write a Python Program to count number of uppercase letters, lowercase letters and spaces in a string and toggle case the given string (convert lowercase to uppercase and vice versa).
Examples:
Input : string = 'GeeksforGeeks is a computer Science portal for Geeks' Output : Uppercase - 4 Lowercase - 41 spaces - 7 gEEKSFORGEEKS IS A COMPUTER sCIENCE PORTAL FOR gEEKS Input : string = 'My name is Ayush' Output : Uppercase - 2 Lowercase - 11 spaces - 3 mY NAME IS aYUSH
Algorithm
- Traverse the given string character by character upto its length, check if character is in lowercase or uppercase using built in methods.
- If lowercase, increment its respective counter, convert it to uppercase using upper() function and add it to a new string, if uppercase, increment its respective counter, convert it to lowercase using lower() function and add it to the new string.
- If space, increment its respective counter and add it to a new string
- Print the new string.
Below is the implementation.
# Python code for implementation of upper() # Given string and new string string = 'GeeksforGeeks is a computer Science portal for Geeks' newstring = '' count1 = 0 count2 = 0 count3 = 0 for a in string: # Checking for lowercase letter and # converting to uppercase. if (a.isupper()) = = True : count1 + = 1 newstring + = (a.lower()) # Checking for uppercase letter and # converting to lowercase. elif (a.islower()) = = True : count2 + = 1 newstring + = (a.upper()) # Checking for whitespace letter and # adding it to the new string as it is. elif (a.isspace()) = = True : count3 + = 1 newstring + = a print ( "In original String : " ) print ( "Uppercase -" , count1) print ( "Lowercase -" , count2) print ( "Spaces -" , count3) print ( "After changing cases:" ) print (newstring) |
Output:
In original String : Uppercase - 4 Lowercase - 41 Spaces - 7 After changing cases: gEEKSFORgEEKS IS A COMPUTER sCIENCE PORTAL FOR gEEKS