Register Login

How to Uppercase a String in Python?

Updated Sep 24, 2019

In Python upper() is a build in function which converts lowercase string into uppercase string and returns it. 

In laymen language this method converts all the lowercase character present in string in uppercase character, if there is no lowercase character present in the given string it will return the original string.

Syntax:

string.upper()

Parameters: Does not take any parameters 

Returns: Returns uppercase string, if no lower case character present it return input string.

Example:

# Python code to explain upper() function 
  
# Initialize a string with all lowercase characters 
str1 = 'stechies.com'
print(str1.upper())

# Initialize a string with uppercase & lowercase characters 
str1 = 'StecHIes.com'
print(str1.upper())

Output:

STECHIES.COM
STECHIES.COM

Use of upper() function in application

Example:

# Python program to compare two string using upper() function

# Initialize strings to compare
str1 = 'STECHIES'
str2 = 'SteChies'

# Compare string without using upper() function
if(str1 == str2):
    print(str1,'and ',str2,' are same')
else:
    print(str1,'and ',str2,' are not same')
    
# Compare string with upper() function
if(str1.upper() == str2.upper()):
    print(str1,'and ',str2,' are same')
else:
    print(str1,'and ',str2,' are not same')

Output:

STECHIES and  SteChies  are not same
STECHIES and  SteChies  are same

As we know that python is a case sensitive programming language so it treats capital “S” and small ‘s’ as two different characters.

So to compare string we first need to convert both the string in either small or capital letter. 

isupper() function

In python isupper() is a build in function, this function checks whether all the characters present in a string is uppercase or not.

Syntax:

string.isupper()

Parameters: Does not take any parameters 

Returns:

True: if all characters are uppercase
False: if one or more character are lowercase

Example:

# Python program to compare two string using lower() function

# Initialize strings
str1 = 'STECHIES'
str2 = 'SteChies'
str3 = 'stechies'

# Check if strings are in uppercase or not
print('String 1 STECHIES Upper: ', str1.isupper())
print('String 2 SteChies Upper: ', str2.isupper())
print('String 3 stechies Upper: ', str3.isupper())

Output:

String 1 STECHIES Upper:  True
String 2 SteChies Upper:  False
String 3 stechies Upper:  False

 


×