Open In App

isupper(), islower(), lower(), upper() in Python and their applications

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss isupper(), islower(), upper(), and lower() functions in Python. These methods are built-in methods used for handling strings. Before studying isupper(), islower(), upper(), and lower() in detail let’s get a basic idea about them.

What is isupper() in Python?

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”

  1. It returns “True” for whitespaces but if there is only whitespace in the string then returns “False”.
  2. It does not take any arguments, Therefore, It returns an error if a parameter is passed.
  3. Digits and symbols return “True” but if the string contains only digits and numbers then returns “False”

This function is used to check if the argument contains any uppercase characters such as:

Input: string = 'GEEKSFORGEEKS'
Output: True

Syntax of isupper()

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.

Example: Checking for Uppercase Characters

In this code string variable contain value “GEEKSFORGEEKS” . String consist of only uppercase letters. When this call the “isupper”method on string , It will return “True” and when it checks the other string “GeeksforGeeks” that is mix of upper and lower string it will return “False“.

Python3




string = 'GEEKSFORGEEKS' # Define a string containing only uppercase letters
print(string.isupper())  # Check if all characters in the string are uppercase and print the result
  
string = 'GeeksforGeeks'# Define a string with a mix of uppercase and lowercase letters
print(string.isupper()) # Check if all characters in the string are uppercase and print the result


Output:

True
False

What is islower() in Python?

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”. 

  1. It returns “True” for whitespaces but if there is only whitespace in the string then returns “False”.
  2. It does not take any arguments, Therefore, It returns an error if a parameter is passed.
  3. Digits and symbols return “True” but if the string contains only digits and numbers then returns “False”.

This function is used to check if the argument contains any lowercase characters such as :

Input: string = 'geeksforgeeks'
Output: True

Syntax of islower()

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.

Example: Checking for Lowercase Characters

This code is helps to check whether a string is entirely composed of lowercase letters or not. Using “islower()” method, that is a built-in method in Python’s string class. If the string contains only lowercase then it will return “True” otherwise it will return “False”.

Python3




string = 'geeksforgeeks'# Define a string containing only lowercase letters
print(string.islower()) # Check if all characters in the string are lowercase and print the result
  
string = 'GeeksforGeeks' # Define a string with a mix of uppercase and lowercase let
print(string.islower())  # Check if all characters in the string are lowercase and print the result


Output:

True
False

What is lower() in Python?

In Python, lower() is a built-in method used for string handling. The lower() method returns the lowercased string from the given string. It converts all uppercase characters to lowercase python. If no uppercase characters exist, it returns the original string. 

  1. It does not take any arguments, Therefore, It returns an error if a parameter is passed.
  2. Digits and symbols return are returned as it is, Only an uppercase letter is returned after converting to lowercase in Python.
Input: string = 'GEEKSFORGEEKS'
Output: geeksforgeeks

Syntax of lower()

Syntax: string.lower()

Parameters:

  • lower() does not take any parameters

Returns: It converts the given string in into lowercase and returns the string.

Examples

In this code we will use the lower() method to convert the strings to lowercase. Firstly we will take uppercase string”GEEKSFORGEEKS” that is converted to lower case() with the help of string.lower() function. Same we will try with the string that contains both upper and lower case then function will convert this to lower case.

Python3




# Checking for lowercase characters
string = 'GEEKSFORGEEKS' #Define a string that contains only uppercase.
print(string.lower()) #convert into lower case
  
string = 'GeeksforGeeks' #Define a string that contains noth uppercase and lowercase.
print(string.lower()) #convert into lower case.


Output:

geeksforgeeks
geeksforgeeks

What is upper() in Python?

In Python, upper() is a built-in method used for string handling. The upper() method 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. 

  1. It does not take any arguments, Therefore, It returns an error if a parameter is passed.
  2. Digits and symbols return are returned as it is, Only a lowercase letter is returned after converting to uppercase.
Input: string = 'geeksforgeeks'
Output: GEEKSFORGEEKS

Syntax of upper()

Syntax: string.upper()

Parameters:

  • upper() does not take any parameters

Returns: It converts the given string in into uppercase and returns the string.

Example

In this code we will use upper() method to convert the strings to uppercase. Firstly we will take lowercase string”geeksforgeeks” that is converted to uppercase() with the help of string.upper() function. Same we will try with the string that contains both upper and lower case “My name is ayush” then function will convert this to lower case.

Python3




# checking for uppercase characters
string = 'geeksforgeeks' #Define a string that contains only lowercase()
print(string.upper()) #Convert into uppercase
  
string = 'My name is ayush' #Define a string that contains only lower case
print(string.upper()) #convert into uppercase.


Output:

GEEKSFORGEEKS
MY NAME IS AYUSH

Count uppercase, lowercase letters, and spaces

Given a string, the task is to write a Python Program to count a number of uppercase letters, lowercase letters, and spaces in a string and toggle case the given string (convert lowercase to uppercase and vice versa).

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

Example

Traverse the given string character by character up to its length, and check if the character is in lowercase or uppercase using built-in methods. If lowercase, increment its respective counter, convert it to uppercase using the upper() function and add it to a new string, if uppercase, increment its respective counter, convert it to lowercase using the 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.

Python3




string = 'GeeksforGeeks is a computer Science portal for Geeks'
newstring = ''
count1 = 0
count2 = 0
count3 = 0
  
for a in string:
    # converting to uppercase.
    if (a.isupper()) == True:
        count1 += 1
        newstring += (a.lower())
    # converting to lowercase.
    elif (a.islower()) == True:
        count2 += 1
        newstring += (a.upper())
  
    # 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


Last Updated : 24 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads