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”.
- 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”
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'
print (string.isupper())
string = 'GeeksforGeeks'
print (string.isupper())
|
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”.
- 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”.
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'
print (string.islower())
string = 'GeeksforGeeks'
print (string.islower())
|
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.
- 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 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
string = 'GEEKSFORGEEKS'
print (string.lower())
string = 'GeeksforGeeks'
print (string.lower())
|
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.
- 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.
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
string = 'geeksforgeeks'
print (string.upper())
string = 'My name is ayush'
print (string.upper())
|
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:
if (a.isupper()) = = True :
count1 + = 1
newstring + = (a.lower())
elif (a.islower()) = = True :
count2 + = 1
newstring + = (a.upper())
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!