W3Basic Logo

Python String upper() Method

The str.upper() method converts all the characters of a string from lowercase to uppercase and returns the copy of a string. If the string does not have lowercase characters, it returns the original string.

Example:

text = "python is awesome"
print(text.upper())

Output

PYTHON IS AWESOME

Syntax

The Syntax of the str.upper() function is:

str.upper()

Here, str is a string.

Parameters

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

Return Value

The str.upper() function returns the copy of an uppercased string. It converts all the lowercase characters into uppercase, and if the string does not contain lowercase characters, it returns the original string as-is.

Example 1: Python Program to convert string from lowercase to uppercase

# Text with Mixed Case
text = "python is both Compiled as well as an Interpreted Language"
print(text.upper())

# Alphanumeric with special characters Text
text = "There are 7 DAYS in a WEEK !!!"
print(text.upper())

Output

PYTHON IS BOTH COMPILED AS WELL AS AN INTERPRETED LANGUAGE
THERE ARE 7 DAYS IN A WEEK !!!

Example 2: Convert all the elements in the list to uppercase

The Program iterates all the elements in the list and prints them in uppercase.

programming_languages = ["python","c#","go","java","javascript"]

for ele in programming_languages:
    print(ele.upper())

Output

PYTHON
C#
GO
JAVA
JAVASCRIPT

Example 3: How upper() method is used in the Program

Let's say if you have an application that stores the first name in uppercase and if the user enters their first name in lowercase or title case, you first convert them into uppercase using the upper() method and check if it exists in the Database.

employee_name = ["JACK", "TOM", "JOE", "RACHEL"]

fname = input("Enter your firstname:")
if fname.upper() in employee_name:
    print("FirstName Found")
else:
    print("Invalid First Name")

Output

Enter your firstname:JaCk
FirstName Found

Enter your firstname:Jim
Invalid First Name

Reference: Python Official Docs

© 2023 W3Basic. All rights reserved.

Follow Us: