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

Tutoriel de base Python

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python string upper() usage and examples

Python string methods

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

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.

Example1Convert the 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!

Example2How 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