What is the ascii_uppercase constant in Python?

The string module in Python

The string module in Python is a collection of different constants.

The ascii_uppercase constant

The ascii_uppercase constant in the string module contains the English letters from A to Z only in uppercase as a string.

The value of the constant is as follows:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Syntax

string.ascii_uppercase

As ascii_uppercase is a constant, we can access it via the string module name.

Code example 1

import string
ascii_uppercase_output = string.ascii_uppercase
print("string.ascii_uppercase = '%s'" % (ascii_uppercase_output))

Code explanation

  • Line 1: We import the string module.
  • Line 3: We store the output of string. ascii_uppercase in the ascii_uppercase_output variable.
  • Line 5: We print ascii_uppercase_output.

Code example 2

import string
def is_ascii_uppercase_only(str_input):
for i in str_input:
if i not in string.ascii_uppercase:
return False
return True
str_to_check_1 = "ADFVERFDD"
print("Does %s contains only ascii uppercase letters? %s" % (str_to_check_1, is_ascii_uppercase_only(str_to_check_1)))
str_to_check_2 = "abji232daosfdgfRFDFD"
print("Does %s contains only ascii uppercase letters? %s" % (str_to_check_2, is_ascii_uppercase_only(str_to_check_2)))

Code explanation

  • Line 1: We import the string module.
  • Lines 3–9: We define a method called is_ascii_uppercase_only that accepts a string as parameters and checks whether or not the input string contains only uppercase ASCII letters.
  • Line 11: We define a string called str_to_check_1 containing only ASCII uppercase letters.
  • Line 12: We invoke the is_ascii_uppercase_only method passing str_to_check_1 as a parameter. Then, we display the results.
  • Line 14: We define a string called str_to_check_2 containing lowercase and uppercase ASCII letters and digits.
  • Line 15: We invoke the is_ascii_uppercase_only method passing str_to_check_2 as a parameter. Then, we display the results.

Free Resources