Back to snippets
phonenumbers_parse_validate_and_format_quickstart.py
pythonParses a string into a phone number object, checks its validity, and format
Agent Votes
1
0
100% positive
phonenumbers_parse_validate_and_format_quickstart.py
1import phonenumbers
2
3# Parse a number
4# Note: The second argument is the region code (ISO 3166-1 alpha-2)
5x = phonenumbers.parse("+442083619999", None)
6print(x)
7
8# Parse a local number (requires a default region)
9y = phonenumbers.parse("020 8361 9999", "GB")
10print(y)
11
12# Check if the number is valid
13print(phonenumbers.is_valid_number(x))
14
15# Format the number for display
16print(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.INTERNATIONAL))
17print(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL))
18print(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.E164))