clean_phonenumber

clean_phonenumber

Functions

Name Description
clean_phonenumber Clean and validate a free-text entry phone number and convert it to the standard Canadian format “+1 (XXX) XXX-XXXX”.

clean_phonenumber

clean_phonenumber.clean_phonenumber(text)

Clean and validate a free-text entry phone number and convert it to the standard Canadian format “+1 (XXX) XXX-XXXX”.

The function accepts phone number in a variety of formats, including with or without parentheses, spaces, dashes.

Parameters

Name Type Description Default
text str The input string representing a Canadian phone number. required

Returns

Name Type Description
text str The validated Canadian phone number in standard “+1 (XXX) XXX-XXXX” format.

Raises

Name Type Description
ValueError If the input does not contain exactly 10 digits.
TypeError The input must be provided as a string.

Examples

>>> clean_phonenumber("123-456-7890")
'+1 (123) 456-7890'
>>> clean_phonenumber("1234567890")
'+1 (123) 456-7890'
>>> clean_phonenumber("(123) 456-7890")
'+1 (123) 456-7890'
>>> clean_phonenumber(" 123 456 7890 ")
'+1 (123) 456-7890'
>>> clean_phonenumber("123456")
# Raises ValueError: Invalid phone number length
>>> clean_phonenumber(1234567890)
# Raises TypeError: Phone number must be provided as a string