Change language

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

| | |

In Python, isupper () — it is a built-in method used to process strings. 
The isupper () methods return "True" if all characters in the string are uppercase, otherwise it 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:  1.True- If all characters in the string are uppercase. 2.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

  1. Returns "True" for spaces
  2. It takes no arguments, so it returns an error if a parameter is passed.
  3. Digits and characters return "True", only capital letter returns "false".

# Python code to implement isupper ()

 
# checking capital letters

string = ’GEEKSFORGEEKS’

print (string.isupper ())

  

string = ’ GeeksforGeeks’

print (string.isupper ())

Output:

 True False 

ISLOWER ()

In Python islower () — it is a built-in method used to process strings. 
The islower () methods return "True" if all characters in the string are lowercase, otherwise it 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:  1.True- If all characters in the string are lower. 2.False- If the string contains 1 or more non-lowercase characters. 

Examples:

 Input: string = ’pythonengineering’ Output: True Input: string =’ GeeksforGeeks’ Output: False 

Errors and exceptions

  1. Returns "True" for spaces
  2. It takes no arguments, so it returns an error if a parameter is passed.
  3. Digits and characters return "True", only a lowercase letter returns "false".

# Python code to implement isupper ()

 
# checking lowercase letters

string = ’pythonengineering’

print (string.islower ())

  

string = ’GeeksforGeeks’

print ( string.islower ())

Output:

 True False 

below ()

In Python, lower () — it is a built-in method used to process strings. 
The lower () methods return a lowercase string from a given string. It converts all uppercase letters to lowercase. If no uppercase characters exist, the original string is returned.

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: pythonengineering Input: string =’ GeeksforGeeks’ Output: pythonengineering 

Errors and exceptions

  1. It takes no arguments, so it returns an error if a parameter is passed.
  2. The numbers and characters returned are returned as-is. After converting to lowercase, only the uppercase letter is returned.

# Python code to implement lower ()

 
# Checking lowercase letters

string = ’GEEKSFORGEEKS’

print (string.lower ())

 

string = ’GeeksforGeeks’

print (string.lower ())

Output:

 pythonengineering pythonengineering 

Top ()

In Python, upper () — it is a built-in method used to process strings. 
The upper () methods return the uppercase string from the given string. It converts all lowercase letters to uppercase. If no lowercase characters exist, the original string is returned.

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 = ’pythonengineering’ Output: GEEKSFORGEEKS Input: string =’ My name is ayush’ Output: MY NAME IS AYUSH 

Errors and Exceptions

  1. It takes no arguments, so it returns an error if a parameter is passed.
  2. Returned digits and the characters are returned as is. After conversion to uppercase, only the lowercase letter is returned.

Output:

 GEEKSFORGEEKS MY NAME IS AYUSH 

Usage: For a given row in python count count in uppercase letters, lowercase letters and spaces in a line and switch the case of the given string (convert lowercase letters to uppercase and vice versa).

Examples :

 Input: string = ’Python.Engineering 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
1. Go through the given line character by character to its length, check if the character is lowercase or uppercase using built-in methods. 
2. If lower case, increment its corresponding counter, convert it to upper case using the upper () function, and add it on a new line, if upper case, increment its corresponding counter, convert it to lower case using the lower () function, and add it on a new line. 
3. If there is a space, increase its corresponding counter and add it on a new line.
4. Type a new line.

# Python code to implement upper ()

 
# checking capital letters

string = ’pythonengineering’

print (string.upper ())

 

string = ’My name is ayush’

print (string.upper ())

Output:

 In original String: Uppercase - 4 Lowercase - 41 Spaces - 7 After changing cases: gEEKSFORgEEKS IS A COMPUTER sCIENCE PORTAL FOR gEEKS 

# Python- code to implement upper ()
# Specified line and newline

 

string = ’Python.Engineering is a computer Science portal for Geeks’

newstring = ’’

count1 = 0

count2 = 0

count3 = 0

  

for a in string:

# Check lowercase letters and convert to uppercase.

if (a.isupper ()) = = True :

count1 + = 1

newstring + = (a.lower ())

# Check capital letters and convert to lowercase.

elif (a.islower ()) = = True :

count2 + = 1

newstring + = (a.upper ())

# Check for missing letters and add them on a new line as 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)