What is the octdigits constant in Python?
Overview
The string module in Python is a collection of different constants. The octdigits constant in the string module contains all the ASCII octadecimal digits. The value of the constant is 01234567.
Syntax
Since octdigits is a constant, we can access it via the string module name.
The syntax for using the octdigits constant is as follows:
string.octdigits
Note: We need to import the
stringlibrary to use theoctdigitsconstant.
Let’s look at examples using the octdigits constant.
Example 1
import stringoctdigits_output = string.octdigitsprint("string.octdigits = '%s'" % (octdigits_output))
Explanation
- Line 1: We import the
stringmodule. - Line 3: We store the output of
string.octdigitsin theoctdigits_outputvariable. - Line 5: We print
octdigits_output.
Example 2
import stringdef is_octdigits_only(str_input):for i in str_input:if i not in string.octdigits:return Falsereturn Truestr_to_check_1 = "6543454324100"print("Does %s contains only octadecimal digits? %s" % (str_to_check_1, is_octdigits_only(str_to_check_1)))str_to_check_2 = "abji232daosfdgfRFDFD"print("Does %s contains only octadecimal digits? %s" % (str_to_check_2, is_octdigits_only(str_to_check_2)))
Explanation
- Line 1: We import the
stringmodule. - Lines 3–9: We define a method called
is_octdigits_only()that accepts a string as a parameter. It checks whether or not the input string contains only ASCII octadecimal digits. - Line 11: We define a string called
str_to_check_1that contains only ASCII octadecimal digits. - Line 12: We invoke the
is_octdigits_only()method, and passstr_to_check_1as a parameter. - Line 14: We define a string called
str_to_check_2that contains lowercase and uppercase ASCII letters and digits. - Line 15: We invoke the
is_octdigits_onlymethod, and passstr_to_check_2as a parameter.