Free cookie consent management tool by TermsFeed The upper() method of str class in Python | Pythontic.com

The upper() method of str class in Python

Method Name:

upper

Method Signature:

upper()

Return Value:

A string of all uppercase characters. 

Method Overview:

  • For a given string of characters the method uppercase() returns a string whose characters are all in uppercase.
  • A Python string is a sequence of Unicode characters. Hence the uppercase() method can convert any string consiting of lowercase characters from languages like Russian into uppercase as given in the Example 2.

Example 1:

# Example Python program that converts strings 
# of various cases to uppercase

# Lowercase to uppercase
txt = "warning";
print("Lowercase:%s"%txt);
print("Uppercase:%s"%txt.upper());

# Titlecase to uppercase
txt = "New York City";
print("Titlecase:%s"%txt);
print("Uppercase:%s"%txt.upper());

# Mixed to uppercase
txt = "Lakes of the world";
print("Mixed case:%s"%txt);
print("Uppercase:%s"%txt.upper());

Output:

Lowercase:warning

Uppercase:WARNING

Titlecase:New York City

Uppercase:NEW YORK CITY

Mixed case:Lakes of the world

Uppercase:LAKES OF THE WORLD

Example 2 - Convert a string of Cyrillic uppercase alphabets to lowercase:

# Example Python program that converts lowercase 
# unicode characters into uppercase unicode 
# characters using str.upper()

# Hello world - Russian
lowercaseUnicode ="привет, мир!"

# Convert to upper case - Russian
uppercaseUnicode = lowercaseUnicode.upper()

print("Hello world in Russian(in lower case):")
print(lowercaseUnicode)

print("Hello world in Russian(in upper case):")
print(uppercaseUnicode)

print("Unicode | In decimal | In hexadecimal")
# The unicode values for Cyrillic lower case letters
for char in lowercaseUnicode:
    print("{}-{}-{}".format(char, ord(char), hex(ord(char))))

print("Unicode | In decimal | In hexadecimal")
# The unicode values for Cyrillic upper case letters
for char in uppercaseUnicode:
    print("{}-{}-{}".format(char, ord(char), hex(ord(char))))

Output:

Hello world in Russian(in lower case):
привет, мир!
Hello world in Russian(in upper case):
ПРИВЕТ, МИР!
Unicode | In decimal | In hexadecimal
п-1087-0x43f
р-1088-0x440
и-1080-0x438
в-1074-0x432
е-1077-0x435
т-1090-0x442
,-44-0x2c
 -32-0x20
м-1084-0x43c
и-1080-0x438
р-1088-0x440
!-33-0x21
Unicode | In decimal | In hexadecimal
П-1055-0x41f
Р-1056-0x420
И-1048-0x418
В-1042-0x412
Е-1045-0x415
Т-1058-0x422
,-44-0x2c
 -32-0x20
М-1052-0x41c
И-1048-0x418
Р-1056-0x420
!-33-0x21

 


Copyright 2025 © pythontic.com