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 stringascii_uppercase_output = string.ascii_uppercaseprint("string.ascii_uppercase = '%s'" % (ascii_uppercase_output))
Code explanation
- Line 1: We import the
stringmodule. - Line 3: We store the output of
string. ascii_uppercasein theascii_uppercase_outputvariable. - Line 5: We print
ascii_uppercase_output.
Code example 2
import stringdef is_ascii_uppercase_only(str_input):for i in str_input:if i not in string.ascii_uppercase:return Falsereturn Truestr_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
stringmodule. - Lines 3–9: We define a method called
is_ascii_uppercase_onlythat 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_1containing only ASCII uppercase letters. - Line 12: We invoke the
is_ascii_uppercase_onlymethod passingstr_to_check_1as a parameter. Then, we display the results. - Line 14: We define a string called
str_to_check_2containing lowercase and uppercase ASCII letters and digits. - Line 15: We invoke the
is_ascii_uppercase_onlymethod passingstr_to_check_2as a parameter. Then, we display the results.