Input Function in Python

Introduction

In Python, the input function is a handy tool for getting information from the user. It pauses your program and displays a message (optional) asking the user to enter some data. Whatever the user types in, input() captures it and returns it as a string, even if the user enters a number.

For example:

name = input("Enter your name: ")
print("Hello,", name)

This code prompts the user to enter their name, stores it in the name variable (as a string), and then greets the user by name.

Remember, the input function always gives you a string, even if the user types in a number. To use that data as a number, you’ll need to convert it using functions like ‘int‘ for integers or ‘float’ for decimals.

For example:

age = int(input("What is your age? "))

In this example, the input function is used to prompt the user for their age. The input is then converted to an integer using the ‘int‘ function and stored in the variable ‘age‘.

If the user enters invalid data, such as a string when an integer is expected, the program will raise a ValueError. To handle such situations, it is important to use error-handling techniques like try-except statements.

Example of using a try-except statement:

try:
    age = int(input("What is your age? "))
except ValueError:
    print("Invalid input. Please enter a valid integer.")

In this example, the try-except statement is used to catch the ValueError that may be raised if the user enters invalid data. If a ValueError is raised, the program will print an error message asking the user to enter a valid integer.

Multiple Inputs

While the input function normally reads one line of text, you can take multiple inputs in a single line by using the ‘split‘ method. This method splits the user’s input based on a separator (delimiter) you define. For example, using a comma (,) as the separator, the user would enter values separated by commas.

For example, suppose you want to receive a person’s name and age simultaneously. In that case, you can declare two variables, ‘name‘ and ‘age‘ in a single line and apply the ‘split()‘ function to separate the two values with a comma as the delimiter.

name, age = input("Enter Name & Age separated by a comma: ").split(",")
print(f"Name is: {name} & Age is: {age}")

Exercises

Try some exercises for practicing the Python input function:

  1. Write a program that asks the user for their name and greets them by name.
  2. Write a program that asks the user to enter two numbers and calculates their sum, difference, product, and quotient.
  3. Write a program that takes a comma-separated number from the user and prints the sum of those numbers.
  4. Write a program that prompts the user for an integer input and prints that.

These exercises would help you practice using Python’s input function and build your programming skills.

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 194