How to Get Keyboard Input in Python – From…
In this short post, you will learn how to get input from user via the keyboard. Specifically, you will learn how use Python’s input() method to enable a user to type in strings and integers.
Syntax of the input() Function
Now, before we continue and learn how to get keyboard input from the user in Python here’s the syntax of the input() function:
input(prompt)
In the input() syntax, above, the prompt is refering to a string that will be printed on the console. and the control is given to the user to enter the value. Importantly, here youshould print some useful information so that the user will input the expected value!
If you have the need to import data from a csv file, this is also possible in Python, and you can check that post out.
Steps to Getting Keybord Input
Now, before we go into detail on how to get keyboard input from a user in Python we will go through the different steps:
1. Create your Python Script
First, you need to start up your favorite text editor, or IDE, and create your a new Python script. If you alreade have a Python script you can skip to the next section.
2. Assign the Input from the User to a Variable
Next, you will need to create a variable using input():
val = input('Please type some text and press ENTER: \n')
3. Use the Input Obtained from the User
Now, you the input from the user and can use it for something. For instance, if you just want to get the user feedback you can print the input:
print(f'You typed: {val}')
Now, if the user enters a string containing punctuation marks, I refer to the recent post on how to remove punctuation in Python. In that post, you will find the fastest method (out of the three tested) for removing punctation marks with Python!
What is the Type of User-Entered Value?
In this section, of the how to get user input in Python, you will learn how to learn what the data type the input. Hint, no matter what the user type it will be of the string type. Now, this will be evident if you run the example code where we get the user input in Python:
val = input('Please type one, or many, numbers and press ENTER: \n')
print(f'You typed: {val}')
print(f'And this is of the {type(val)} data type')
Changing the Type of the User Input in Python
Now, in this section you will learn how to change the type, from string to integer, when getting user input from keyboard in Python. It’s simple you will add the int() method:
val = input('Please type one, or many, numbers and press ENTER: \n')
val = int(val)
print(f'You typed: {val}')
print(f'And this is of the {type(val)} data type')
Note, that if the user is entering a string, when running the above code, the script will through a ValueError:
How to Get an Integer as the User Input and Handle Error?
Now, you can add some basic error handling to avoid the ValueError when getting input from a user using Python. In the get input from user in Python example below, we will use a while() loop, try- and except to get the correct input from the user:
correct_val = True
while correct_val:
val = input('Please type one, or many, numbers and press ENTER: \n')
try:
val = int(val)
correct_val = False
except ValueError:
print(f'You typed in a {type(val)}. Please type a \
NUMBER (or many numbers)')
print(f'You typed: {val}')
print(f'And this is of the {type(val)} data type')
Python User Input Choice Example
Finally, in the last example you will learn how to get the user input in Python and have the script different things depending on user input.
correct_val = True
while correct_val:
val1 = input('Please enter the first number and press ENTER: \n')
val2 = input('Please enter the second number and press ENTER: \n')
choice = input('Please enter 1 for division or \n 2 for multiplication.')
try:
val1 = int(val1)
val2 = int(val2)
choice = int(choice)
correct_val = False
except ValueError:
print(f'You typed in a {type(val1)} and a {type(val2)}. Please type a \
NUMBER (or many numbers)')
if choice == 1:
print(f'You typed in {val1} and {val2} and their division is: \
{val1 / val2}')
elif choice == 2:
print(f'You typed in {val1} and {val2} and their multiplication is: \
{val1 * val2}')
If you need to learn more about Python and the syntax, while loops, and if-statements and so on, check the previous post out.
Conclusion: Get User Input in Python
In this post, you have learned how to get user input in Python. In fact, you have learned how to change string input to integer input, handle errors (ValueError) and how to get multiple choices from user input in Python. Hope you enjoyed it and learned something. If there is something you want to learned concerning how to get user input in Python, or anything else Python related, drop a comment below!