Uppercase and lowercase letters in python

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

Input: string = 'GeeksforGeeks'
Output: False

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:

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

  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

Input: string = 'GeeksforGeeks'
Output: False

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.

Uppercase and lowercase letters in python
Example:

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. 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.
Input: string = 'GEEKSFORGEEKS'
Output: geeksforgeeks

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:

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. 

  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

Input: string = 'My name is ayush'
Output: MY NAME IS AYUSH

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:

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

Input : string = 'My name is Ayush'
Output : Uppercase - 2
         Lowercase - 11
         spaces - 3
         mY NAME IS aYUSH

Algorithm

  1. 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.
  2. 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.
  3. If space, increment its respective counter and add it to a new string
  4. Print the new string.

Example:

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

How do I allow both lower and uppercase input in Python?

upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.

How do you uppercase a letter in Python?

upper() Return Value upper() method returns the uppercase string from the given string. It converts all lowercase characters to uppercase.

Does uppercase and lowercase matter in Python?

Variables can only contain upper and lowercase letters (Python is case-sensitive) and _ (the underscore character). Hence, because we can't have spaces in variable names a common convention is to capitalize the first letter of every word after the first. For example, myName, or debtAmountWithInterest.