Upper function in Python string | Python string uppercase() method with examples

In this Python tutorial, we will see what is upper function in Python string and its application with examples. I will show you, how to use the Python string uppercase() method with a few examples.

We will also see, how to capitalize each word in a string in Python.

What is the Upper() Function in Python

In Python, the upper() function is a built-in string method used to convert all the lowercase characters in a string to uppercase.

Syntax, Parameters and Return values

It is very important to know that what way we should apply the Upper() function in Python, that does not take us to an error.

The syntax:

The syntax of the upper function in Python is:

string.upper()

Parameter:

The Python upper() function does not take any parameters.

Return values:

The Python upper() returns the uppercase string from the given Python string. It converts all the lowercase characters to uppercase and does not affect any characters in the string that are not letters.

Python upper() function using examples

The upper() function in Python has a variety of applications. Here are some practical examples that demonstrate its usage:

Example-1: Convert Python string to Uppercase

The basic application of upper() function in Python is to convert the whole string to uppercase.

For instance, Imagine working with a Python string that includes the names of some states in the United States. The data may have been entered inconsistently – some in all caps, some in title case, and some in lowercase.

To bring uniformity to the string in Python, we can use the upper() function to convert all state names to uppercase:

states = "california, TEXAS, new York, FLORIDA, maSSACHUsetts"

states_upper = states.upper()

print(states_upper)

The output is:

CALIFORNIA, TEXAS, NEW YORK, FLORIDA, MASSACHUSETTS
The upper function in Python string

This we use string.upper() method in Python to make the given string uppercase.

Read: Capitalize function in Python string

Example-2: Converting User Input to Uppercase

Sometimes its become very important that whatever we are getting data from a user input should be in same format. To make the that happen we can use the upper() function Python to store the data in the uppercase for all situation.

Consider a scenario where we’re creating a registration form in Python. To avoid inconsistencies and ease the validation process, it might be helpful to standardize user input in Python.

This can be especially important with entries such as gender (Male, male, MALE) or country name (United States, united states, UNITED STATES).

user_gender = input("Enter your gender: ")
user_country = input("Enter your country: ")

user_gender = user_gender.upper()
user_country = user_country.upper()

print("Gender:", user_gender)
print("Country:", user_country)

The output is:

Enter your gender: sAmmy
Enter your country: United STates
Gender: SAMMY
Country: UNITED STATES

As we have the entered the data as a mixture of upper and lower case but in the Python is converting the data using upper function and printing that data.

string.upper() in Python with user input

This way we can use Python upper function to convert the user input data in one case.

Example-3: Creating a Case-Insensitive Password

We can use the Python upper() function in insensitive cases where we will check whether the input data is relevant or not.

Consider a case where we’re developing a password validation function in Python. We’ve decided that passwords should be case-insensitive, meaning “PASSWORD”, “password”, and “Password” should all be treated the same. This can be achieved by using the string upper() function :

original_password = 'Password'.upper()

def validate_password(input_password):
    if input_password.upper() == original_password:
        return True
    else:
        return False

print(validate_password('PASSWORD'))
print(validate_password('password'))
print(validate_password('PassWord'))
print(validate_password('pass'))

The output is:

True
True
True
False

The last one is false because it is not the full password.

Python upper() function in string in case insensitive problem

This way we can use upper method in Python string for case insensitivity.

Capitalize each word in a string in Python

In python, to capitalize each word in a string we will use the built-in method upper() to capitalize all the letters in the string.

Example:

my_string = 'python guides'
print(my_string.upper())

After writing the above Python code (python capitalize each word in a string), Ones you will print “my_string.upper()” then the output will appear ” PYTHON GUIDES “. Here, the string.upper() is used to capitalize each word in the string. Also, you can refer to the below screenshot to capitalize each word in a string.

Python capitalize each word in a string
Python capitalize each word in a string

This is another Python string formatting example, where we saw how to capitalize each word in a string in Python.

This is how to convert a string to uppercase in Python.

Conclusion

In this tutorial, we have learned how to use the Python string uppercase() method. And also we saw, how to capitalize each word in a string in Python. We have seen what is upper() function in Python, its syntax, parameters and return values. The upper function in Python is a powerful tool that can simplify tasks like data cleaning and case-insensitive searches. As demonstrated with our examples.

You may also like the following tutorials: