Program 4: Input Name and Age and Print - 1000+ Python Programs


Question 4)

Write a Program in Python to take the user name and age and print it out on the console.

or

Write a Program in Python using the input function to accept the user name and age and print in the following format.

Hello {name}! Your age is {age}


Code Example:

# Program 4
# Print user name and age as input
# 1000+ Python Programs by Code2care.org

user_name = input("Enter your name: ")
user_age = input("Enter your age: ")

print(f"Hello {user_name}, your age is {age}")
Console Output:

Enter your name: Mike
Enter your age: 22
Hello Mike, your age is 22

program-4-print-user-name-age-python

Explanation:

We have made use of two functions here,

  • print() - an inbuild function in Python 3 - read more, we make use of the f-strings along with print function to get the desired result.
  • input(prompt) - an inbuild function again, with the prompt string that is printed out to the standard output without a trailing newline - read more

Varaibles user_name and user_age are used to store the input from the user and then printed using the print function in f-strings format.

Gif Demo Python Program Print Age and Name
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap