upper() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The upper() function in Python is used to convert all lowercase characters of the string to uppercase. It doesn't take any character to change the string. It returns a new string where all the characters of the returned strings are uppercase. If the original string has no character in lowercase, then it will return the original string.

Syntax of upper() function in Python

The syntax of the upper function in python is quite easy. This is explained below.

For the upper function, we just have to use .upper() with our string.

Parameters of upper() function in Python

The upper function doesn't accept any parameter.

Return Value of upper() function in Python

The upper() function in python returns a new string after updating the original string in which all the characters of the original string have been converted to uppercase characters.

If the original string does not have a lowercase character, then it will return the original string.

If we use the upper function on any other data type like an array, or dictionary, it will raise an error.

Example of upper() function in Python

In this example, we will see how we convert the word example from lower case to upper case.

Code:

Output:

Example Of Upper In Python

What is the upper() function in Python?

Ever wondered if your software only accepts the upper case characters and the user enters the lower case characters, then your software will crash. So how can you handle this case?? The upper() function in python is the best way to solve this problem. The upper function converts the string with lowercase characters to a string with only uppercase characters. As we know that strings are immutable, so this function returns a new string after converting the lowercased string to the uppercased string.

More Examples

Example 1: Converting a string to uppercase

In this example, we are converting an entirely lowercase String and semiLowerCase String to the upper case string. We just have to use the upper function in python to tackle this problem.

Code:

Convert a string to uppercase Convert a string to uppercase code output

Output:

Explanation:

  • Firstly, we have created two variables and stored a completely lowercase string and semiLoweredCase string, respectively, to them
  • Then, we use the upper() of python on both strings.
  • Then, we print the updated strings to get the desired answer.

Example 2: Using upper() function to convert mixed string

Let's say we are taking the PAN number of a user as an input, where the user may enter it in any form(whether uppercase, lowercase or a mix of both). By standard convention, PAN Numbers are always represented in uppercase format. So, we need to handle the case of keeping the user's entered PAN Number in the upper case. This can be done with the help of upper() in python.

So in this example, we check whether the user entered PAN Number matches the original PAN Number represented in conventional form.

Code:

Output:

Explanation:

  • Firstly, we have created two variables and stored an entirely lowercase string and perfect upper cased string respectively to them
  • Then, we use the upper() of python on both of the strings and check whether both of the strings are the same or not.
  • At the end, the output of the If condition is returned, representing that both strings are similar.

Conclusion

  • upper() function in python is used to convert the lower case characters of a string to the upper case.
  • It doesn't take any parameter to change the lowercase string to an uppercase string.
  • As we know, strings are immutable, so it will return a new string where all the characters get changed to upper case.

See Also