Python: String upper() Method

A Python string is a collection of characters surrounded by single, double, or triple quotes. The computer does not understand the characters; instead, it stores the manipulated character as a combination of 0’s and 1’s internally.

In this article we are going to discuss the use of upper() function.

String upper() Method in Python

1)Upper() function

The string upper() method returns a string that has all lowercase characters converted to uppercase characters.

Syntax:

given_string.upper()

Parameters:

The upper() method does not accept any parameters.

Return:

The upper() method returns the uppercased version of the given string. All lowercase characters are converted to uppercase.

If no lowercase characters are found, the original string is returned.

2)Converting a string to uppercase

We can convert it using built in function upper().

Below is the implementation:

# given string
string = "btech geeks"
# converting string to upper
string = string.upper()
# print the string
print(string)

Output:

BTECH GEEKS

Explanation:

It returned a copy of the given string, and all lowercase characters were converted to uppercase characters in the returned copy. The uppercase copy of the string was then assigned to the original string.

3)Converting the string which has numbers and letters to uppercase

As the above method we can convert it to uppercase using upper() function.

Below is the implementation:

# given string
string = "btech 2021 geeks"
# converting string to upper
string = string.upper()
# print the string
print(string)

Output:

BTECH 2021 GEEKS

4)Convert two strings to uppercase and compare them

We can convert the given two strings to upper() function and we can compare them using “=” operator.

Below is the implementation:

# given string
string1 = "BTechGeeks"
string2 = "btechgeeks"
# converting both strings to uppercase using upper() function
string1 = string1.lower()
string2 = string2.lower()
# compare the two strings
if(string1 == string2):
    print("Both strings are equal")
else:
    print("Both strings are not equal")

Output:

Both strings are equal

5)Convert character to uppercase

We can convert it using built in function upper().

Below is the implementation:

# given character
character = 'b'
# convert to uppercase using upper() function
character = character.upper()
# print the character
print(character)

Output:

B

Related Programs: