Skip to content
Laviyx Learn

Try

Python Lab · Experiment 5

Read and print a person's details

PythonWeek 1JNTUH R22 · CS206ES

Aim

To write a Python program that reads the name, address, email and phone number of a person from the keyboard and prints the details neatly.

Algorithm

  1. Read the name, address, email and phone number using input(), one after the other.
  2. Remove extra spaces at the start and end of each value with strip().
  3. If the phone number is not exactly 10 digits, print a warning.
  4. Print a heading and then each detail on its own line with a label.

Program

Python
name = input("Enter name: ").strip()
address = input("Enter address: ").strip()
email = input("Enter email: ").strip()
phone = input("Enter phone number: ").strip()

if not (phone.isdigit() and len(phone) == 10):
    print("Warning: phone number should have exactly 10 digits")

print()
print("----- Person Details -----")
print("Name    :", name)
print("Address :", address)
print("Email   :", email)
print("Phone   :", phone)

Output

Output
Enter name: Ravi Kumar
Enter address: 12-3-45, Ameerpet, Hyderabad
Enter email: [email protected]
Enter phone number: 9876543210

----- Person Details -----
Name    : Ravi Kumar
Address : 12-3-45, Ameerpet, Hyderabad
Email   : [email protected]
Phone   : 9876543210

Recorded by running this exact program. Typed input appears after each prompt.

How it works

The phone number is kept as a string, not converted to int. It is a label, not a quantity: we never add or multiply phone numbers, and converting to int would drop a leading 0 or a +91 prefix.

The phone check is a simple one for a 10-digit Indian mobile number. Checking emails and phone numbers properly with regular expressions is a later experiment.

Common mistakes

  • Converting the phone number with int(input()). It fails for numbers written with + or spaces and drops leading zeros. Keep it as a string.

  • Using several input() calls on one line and getting the order of values mixed up. Read one value per statement with a clear prompt.

  • Reading all four details with one input() and split(). An address has spaces and commas inside it, so splitting breaks it into pieces. Read each detail with its own input().

Viva questions

Try to answer each one aloud before you open it.

  • What does input() return?

    It returns the line typed by the user as a string, without the newline at the end.

  • Why is the phone number stored as a string and not an int?

    We never do arithmetic on a phone number, and an int would lose a leading zero or a + sign. A string keeps it exactly as typed.

  • What does strip() do?

    It returns a copy of the string with spaces, tabs and newlines removed from the start and the end.

  • What happens when you pass several values to print separated by commas?

    print prints them one after another with a single space between them by default. The separator can be changed with the sep argument.

Learn the concepts

All Python Programming Lab experiments →