Python – User Input

Python allows for user input. That means we are able to ask the user for input either to interact with users and get data to provide some sort of result. Most programs today use a dialog box for that but for console we are using input.

input() function

The input from the user is read as a string and in most cases is assigned to a variable. After entering the value from the keyboard, you have to press the “Enter/Return” button. Then the input() function reads the value entered by the user.

Syntax

variable = input(message)

The message string is displayed on the console and the control is given to the user to enter the value. You should print some useful information to guide the user to enter the expected value.

Example

name = input("What is your name:")
print("Hello " + name+"! Welcome to my Tutorial.")

Input numeric values

favoriteNumber = int(input("What is your favorite number? "))
print("Your favorite number is {}".format(favoriteNumber))

If you put in a non numeric value, you will get this error which means that you input an invalid value for int

ValueError: invalid literal for int() with base 10

Source code on Github




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.