Python
Python Programming for Beginners: A Comprehensive Guide
Python Environment Set Up
Python Basic Syntax
Python Variables
Python Data Types
Python String
Python Numbers
Python Casting
Python List
Python Tuple
Python Set
Python Dictionaries
Python Boolean
Python Arrays
Python Operators
Python For Loop
Python while Loop
Python if elif else
Functions in Python
Python Lambda
Python Classes & Objects
Python Inheritance
Python Scope
Python Modules
Python Math
Python RegEx
Python User Input
Python try except
Python finally
Python Logging
Create a Python Project
Python Managing Application Dependencies
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.
Example:
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.