Uppercase and lowercase 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.

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 you use lowercase and uppercase in Python?

To convert a Python string to lowercase, use the built-in lower[] method of a string. To convert a Python string to uppercase, use the built-in upper[] method.

Does Python distinguish uppercase and lowercase?

A programming language is said to be case sensitive if it distinguishes between uppercase and lowercase letters. Python is a `case sensitive programming language. Variable, functions, modules and package names are written in lowercase by convention. Class names and constants are written in uppercase.

How do you lowercase in Python?

lower[] is a built-in Python method primarily used for string handling. The . lower[] method takes no arguments and returns the lowercased strings from the given string by converting each uppercase character to lowercase. If there are no uppercase characters in the given string, it returns the original string.

What does capitalize [] mean in Python?

The capitalize[] method returns a string where the first character is upper case, and the rest is lower case.

Chủ Đề