How do I print out phone number in python?

Viewed 129

I am creating a contact book using sqlite3 in python, but when I'm trying to insert my phone number as integer type, it appears that it will occur syntax error for octal integer, and when I'm putting the numbers into string type, the 0 is not printed. How can I input my phone number no matter in int or str?

import sqlite3
from math import ceil

conn = sqlite3.connect("contact_book.db")

c = conn.cursor()


c.execute("""CREATE TABLE contact_book(
            name text,
            address text,
            phone_number integer,
            email address text
            )""")

conn.commit()

def insert_contact_details(name, address, phone_number, email_address=None):
    with conn:
        c.execute("INSERT INTO contact_book VALUES(:name, :address, :phone_number, :email_address)",
                  {'name': name, 'address': address, 'phone_number': str(phone_number), 'email_address': email_address})

Below is when I put phone number into int:

insert_contact_details("Durian", "Malaysia", 0123239588)

output:

SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

Below is when I put phone number into str type:

insert_contact_details("Durian", "Malaysia", "0123239588")

output:

[(Durian, Malaysia, 123239588, None)]
2 Answers

The phone number should always be a string/text type everywhere.

Phone numbers in practice are more like strings that happen to use digits, rather than numeric values. For example, "01676 533433" (not a real number) is a valid phone number in my part of the world. But it is not the same as 1676533433. Most languages would consider the leading zero to indicate that this is an octal value.

Phone number must be string type. The reason why is the head of them is zero. Integer on computer has no digit information, so can't express heads zeros.

In addition, in Python, to write 0123 is regarded as octal number 0123 is syntax error simply (thanks Jarvis!), so above error occurred.

Related