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
- Read the name, address, email and phone number using
input(), one after the other. - Remove extra spaces at the start and end of each value with
strip(). - If the phone number is not exactly 10 digits, print a warning.
- Print a heading and then each detail on its own line with a label.
Program
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
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 : 9876543210Recorded 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()andsplit(). An address has spaces and commas inside it, so splitting breaks it into pieces. Read each detail with its owninput().
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
intwould 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?
printprints them one after another with a single space between them by default. The separator can be changed with thesepargument.