In this blog post, we will learn how to prompt a user to provide input and save it into a variable.
Input
Capturing input from users is a very common task, and for that reason, Python has a built-in function. To prompt a user for an input we use the input() function.
Prompt and Print Input
In the following example, I am prompting an input using the input function and saving the result to a variable called value. In the last line of the code, I’m printing the input to the console.
print("Enter a value:", end=' ')
value = input()
print(f"The value that you typed is {value}")
To capture an integer we need to use the following code which will convert string input to an integer.
value = int(input())
With can also be the prompt in the input function as shown below.
value = input("Enter a value:")