input() is a built-in function in Python that allows a user to assign custom input to a variable.
my_cats_name = input("What is your cat's name? \n")print("The best cat in the world is " + my_cats_name)
When this code is run, it will print out “What is your cat’s name?”. Then, the user can type in something and press enter. Whatever the user types will be saved to the variable
my_cats_name and printed out in the final
Once a user inputs a value it can be used like a normal variable. For example, it can be compared against other variables:
answer = input("What is the meaning of life? \n")if answer == "42":print("Good job!")else:print("Sorry! That's not it.")
In this case, when the code is run, it will print out “What is the meaning of life?”. If the user types
"42" and presses
enter, it will print “Good job!”. If the user types anything other than
"42", it will print out “Sorry! That’s not it”.
Whatever the user types will be saved as a string. However, the string can be converted to other types.
my_num = input("What number comes after 1? \n")if my_num == 2:print("Correct!")else:print("No...")# Output: No...
This always prints “No…” because the string
'2' does not equal the number
my_num = input("What number comes after 1? \n")if int(my_num) == 2:print("Correct!")else:print("No...")
With the addition of the
int() type conversion function, this now prints “Correct!” if and only if the user inputs