Check a String is Octal in Python

  • Post category:String

In this article, you will see how to check a string is octal in Python. An octal string is a string that contains only digits 0-7. To check if a string is an octal string, you can use string.octdigits().

Step 1: Import the necessary module

import string

Step 2: Create a string

str = "1234567"
print("str = ", str)
print("str type = ", type(str))

Step 3: Check if the string is octal

if all(c in string.octdigits for c in str):
    print("Octal")
else:
    print("Not Octal")

Output:

str =  1234567
str type =  <class 'str'>
Octal

Free resources to learn advanced skills: AiHints and CodeAllow