User Input in Python

  • Last updated Apr 25, 2024

In Python, you can obtain user input by using the built-in input() function. This function reads the user input and returns it as a string.


user_input = input("Enter your name: ")  # Prompt the user for input

print("You entered:", user_input)  # Display the user's input

In this example, input("Enter your name: ") displays the message "Enter your name: " to the user as a prompt. The user can type their input, press Enter, and that input is stored in the variable user_input as a string.

Here's another simple example of how you can use user input to perform a simple calculation:

n1 = float(input("Enter the first number: "))
n2 = float(input("Enter the second number: "))

result = n1 + n2

print("The result is: ", result)

In this example, the user's input is converted to a float type to perform mathematical operations. The input() function returns a string, so you may need to convert the input to the appropriate data type, such as int or float, depending on your specific requirements.