English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python String upper() Usage and Example

Python String Methods

The string.upper() method converts all lowercase letters in the string to uppercase and returns it.

The syntax of the upper() method is:

string.upper()

string.upper() Parameters

The upper() method does not take any parameters.

upper() Return Value

The upper() method returns the uppercase string from the given string. It converts all lowercase letters to uppercase.

If there are no lowercase letters, return the original string.

Example1:Convert string to uppercase

# Example string
string = "this should be uppercase!"
print(string.upper())
# String with numbers
# Convert all letters to uppercase
string = "Th!s Sh0uLd B3 uPp3rCas3!"
print(string.upper())

When running the program, the output is:

THIS SHOULD BE UPPERCASE!
TH!S SH0ULD B3 UPP3RCAS3!

Example2:How to use upper() in the program?

# First string
firstString = "python is awesome!"
# Second string
secondString = "PyThOn Is AwEsOmE!"
if(firstString.upper() == secondString.upper()):
    print("The strings are the same.")
else:
    print("The strings are different.")

When running the program, the output is:

The strings are the same.

Note:If you want to convert to a lowercase string, please uselower()You can also useswapcase()Swap lowercase and uppercase letters.

Python String Methods