Methods and Functions

Python String upper()

In Python, there are various built-in methods that allow us to easily modify strings. When working with a string in Python, you may want to convert the string to uppercase or check if the string is already in uppercase. This is where Python upper() string function comes in handy. The upper() method converts all case-sensitive characters in a string to uppercase. This article will explain how to use this built-in function to work with uppercase strings. We’ll also go over a few examples to show how they work.

Python upper() function

Definition

  • Python upper() function is an inbuilt string class method that converts all the lowercase characters in the string into uppercase characters and returns a new string.
  • The Python upper() string method returns a string copy with all the characters changed to uppercase. The original string is not altered by this procedure.

Python upper()

Python upper() is a built-in string handling technique. Python’s upper() function can be used to transform all case-based characters in a string to uppercase. The upper() method returns a duplicate of an original string with all characters in uppercase. The upper() function also ignores any numerals, special characters, or uppercase letters in the supplied string.

upper() Syntax

The syntax followed by Python’s upper() function is as follows:

                    

string.upper()

upper() Parameters

The upper() string function does not accept any parameters and it is appended to the end of a string value that already exists.

Note – If any parameter is passed, the function throws an exception error.

Return value from upper()

Python upper() returns the uppercase string from a provided string. All lowercase characters are converted to uppercase. If no lowercase characters are found, the original string is returned.

Example 1: Convert a string to uppercase

Example

                    

# Python program to illustrate upper()
txt = 'Good Morning, Everyone'
print(txt.upper())

txt = 'welcome home jimmy'
print(txt.upper())

txt = 'Mobile No - 9341239145'
print(txt.upper())

txt = 'HELLO'
print(txt.upper())

Output

                    

GOOD MORNING, EVERYONE
WELCOME HOME JIMMY
MOBILE NO - 9341239145
HELLO

Example 2: How upper() works?

Example

                    

# Python program to illustrate upper()
s = input('Enter a string: ')
print('Original string:', s)
print('String in Uppercase:', s.upper())
print('')

my_list = ['John', 'Timmy', 'Becky', 'Emily']
for i in my_list:
    print(i.upper())

Output

                    

Enter a string: I love programming
Original string: I love programming
String in Uppercase: I LOVE PROGRAMMING

JOHN
TIMMY
BECKY
EMILY

Example 3: How upper() is used in programs?

Example

                    

# To check if strings are equal
str1 = 'i love cars'
str2 = 'I LoVe CarS'

if(str1.upper() == str2.upper()):
     print('Both the strings are same')
else:
     print('Both the strings are different')

Output

                    

Both the strings are same

Difference between upper(), capitalize() and isupper()

  • The Python upper() function– The upper() method converts all the lowercase characters to uppercase.
  • Python capitalize() – The capitalize() method only capitalizes the first character of the string.
  • Python isupper() function – This function returns True if all the values in the input string are in uppercase, else it returns False.

Example

                    

txt = 'do you watch tv?'

# converts all to uppercase
print(txt.upper())

# converts first character to uppercase
print(txt.capitalize())

# returns Boolean value
print(txt.isupper())

Output

                    

DO YOU WATCH TV?
Do you watch tv?
False

Frequently Asked Questions

Q1. What is uppercase in Python?

Uppercase is defined as something written or printed in capital letters (i.e. A-Z). To convert any character into uppercase in Python, we use the built-in upper() function. Python upper() function is an inbuilt string class method that converts all the lowercase characters in the string into uppercase characters and returns a new string.

Q2. How do I convert a string to uppercase in Python?

To convert all the string characters to uppercase, we use the Python upper() function. The upper() function in Python can be used to transform all case-based characters in a string to uppercase. Python string upper() method returns a duplicate of an original string with all characters in uppercase

The syntax followed by Python’s upper() function is as follows:

                    

string.upper()

Example

                    

txt = 'Egg Bread Cheese Apple Orange'
print(txt.upper())

txt = 'I love my Country'
print(txt.upper())

txt = 'python programming 101'
print(txt.upper())

Output

                    

EGG BREAD CHEESE APPLE ORANGE
I LOVE MY COUNTRY
PYTHON PROGRAMMING 101

Q3. How do you lowercase all letters in Python?

To convert all the string characters to lowercase, we use the Python lower() function. The lower() function in Python can be used to transform all case-based characters in a string to lowercase. The lower() method returns a duplicate of an original string with all characters in lowercase

The syntax followed by Python’s lower() function is as follows:

                    

string.lower()

Example

                    

txt = 'Egg Bread Cheese Apple Orange'
print(txt.lower())

txt = 'I love my Country'
print(txt.lower())

txt = 'python programming 101'
print(txt.lower())

Output

                    

egg bread cheese apple orange
i love my country
python programming 101

Q4. Difference between upper() and capitalize() in Python?

  • Python’s upper() function – The upper() method converts all the lowercase characters to uppercase.
  • Python’s capitalize() function – The capitalize() method only capitalizes the first character of the string.

Example

                    

txt = 'Egg Bread Cheese Apple Orange'
print(txt.upper())
print(txt.capitalize())
print('')

txt = 'I love my Country'
print(txt.upper())
print(txt.capitalize())
print('')

txt = 'python programming 101'
print(txt.upper())
print(txt.capitalize())

Output

                    

EGG BREAD CHEESE APPLE ORANGE
Egg bread cheese apple orange

I LOVE MY COUNTRY
I love my country

PYTHON PROGRAMMING 101
Python programming 101

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.