What is ascii_lowercase constant in Python?

The string module in Python

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

The ascii_lowercase constant

The ascii_lowercase constant in the string module contains the English alphabets from a to z, only in lowercase, as a string.

The value of the constant is as follows:

abcdefghijklmnopqrstuvwxyz

Syntax

string.ascii_lowercase

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

Code example 1

Let’s look at the code below:

import string
ascii_lowercase_output = string.ascii_lowercase
print("string.ascii_lowercase = '%s'" % (ascii_lowercase_output))

Code explanation

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

Coding example 2

Let’s look at the code below:

import string
def is_ascii_lowercase_only(str_input):
for i in str_input:
if i not in string.ascii_lowercase:
return False
return True
str_to_check_1 = "abjiaosfdgf"
print("Does %s contains only ascii lowercase letters? %s" % (str_to_check_1, is_ascii_lowercase_only(str_to_check_1)))
str_to_check_2 = "abji232daosfdgfRFDFD"
print("Does %s contains only ascii lowercase letters? %s" % (str_to_check_2, is_ascii_lowercase_only(str_to_check_2)))

Code explanation

  • Line 1: We import the string module.
  • Lines 3 to 9: We define a method called is_ascii_lowercase_only that accepts a string as parameters and checks whether the input string contains only lowercase ASCII letters or not.
  • Line 11: We define a string called str_to_check_1 containing only ASCII lowercase letters.
  • Line 12: We invoke the is_ascii_letters_only method, passing str_to_check_1 as a parameter.
  • 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_letters_only method, passing str_to_check_2 as a parameter.

Free Resources