Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the octdigits constant in Python?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 string library to use the octdigits constant.

Let’s look at examples using the octdigits constant.

Example 1

import string
octdigits_output = string.octdigits
print("string.octdigits = '%s'" % (octdigits_output))

Explanation

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

Example 2

import string
def is_octdigits_only(str_input):
for i in str_input:
if i not in string.octdigits:
return False
return True
str_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 string module.
  • 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_1 that contains only ASCII octadecimal digits.
  • Line 12: We invoke the is_octdigits_only() method, and pass str_to_check_1 as a parameter.
  • Line 14: We define a string called str_to_check_2 that contains lowercase and uppercase ASCII letters and digits.
  • Line 15: We invoke the is_octdigits_only method, and pass str_to_check_2 as a parameter.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring