How do I get an input in Python?
How Can We Help?
Teacher,
Cat enthusiast
- The 2022 End of Year Website Update - 01/01/2023
- Celebrating Ada Lovelace day - 10/10/2022
- Website update 5/9/22 - 05/09/2022
To get a user to input something (type it in) Python uses the input statement. What the user inputs is stored into a variable and a prompt can also be displayed.
name = input("Please enter your name ")
print(name)
In this example ‘name’ is a variable which stores what the user types in. The ‘input’ statement tells Python to allow the user to type something into the console. The part after input is simply the prompt that the user will see on the screen which tells them what to enter – in this case we want them to enter their name.
If we only want a user to input a number (because we are using it to calculate something for example) then we need to tell Python that we are expecting a number by adding ‘int’.
number1 = int(input("Enter the first number"))
print(number1)
In this example ‘number1’ is our variable which stores what the user types in. The ‘int’ part of the statement tells Python that we are expecting the user to input a number. The ‘input’ statement tells Python to allow the user to type something into the console. The part after input is simply the prompt that the user will see on the screen which tells them what to enter – in this case we want them to enter the first number.