Exercise 12-07:

Write a program that validates user data entry. It will validate ISBNs (International Standard Book Numbers), which are used to identify books worldwide.
The program must accept ISBNs in the following format: ISBN 999-9-99-999999-9, where each 9 represents a digit.
The ISBN letters are optional and be case-insensitive. The space between N and the first number is mandatory if the ISBN is present. Require the dashes as in the example, verifying the correct number of digits.

Valid values:
ISBN 123-3-12-123456-1
Isbn 123-3-12-123456-1
123-3-12-123456-1

Invalid values:
ISBN123-3-12-123456-1
ISBN 1233-12123456-1

Answer:

##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 12/exercise-12-07.py.py
##############################################################################
# Checks if input is a valid CPF, that is:
# A sequence in the format 999.999.999-99
# This function does not validate the check digit
import re

CPF_RE = r"^\d{3}\.\d{3}\.\d{3}-\d{2}$"


def cpf(input):
    return bool(re.match(CPF_RE, input))


inputs = [
    "123.456.789-01",  # Yes
    "123456.789-01",  # No
    "123.456.78901",  # No
    "23.456.789-01",  # No
    "123.456.78-01",  # No
    "999.999.999-99",  # Yes
]

for input in inputs:
    print(f"{input}: is it a CPF? {'Yes' if cpf(input) else 'No'}")
Click here to download the file