User Input
How to get input from user in Python
In any program, getting user input is essential, whether it's for a simple calculator or an advanced application.
In Python, you can get user input using the built-in input()
function. Here's how you can use it effectively:

Basic Example
name = input("Enter your name: ")
print(f"Hello, {name}!")
input("Enter your name: ")
: Displays a prompt to the user and waits for their input.- Once the user enters value, The entered value is returned as a string.
Converting Input to Other Data Types
The input()
function always returns a string. If you need a different type (e.g., integer or float), you can convert it using
type casting.
Convert to Integer
age = int(input("Enter your age: "))
print(f"You are {age} years old.")
Convert to Float
price = float(input("Enter the price: "))
print(f"The price is ${price:.2f}.")
Convert to Boolean
confirm = input("Do you agree? (yes/no): ").lower() == 'yes'
print(f"Your response: {confirm}")
Handling Multiple Inputs
Space-separated Input
numbers = input("Enter numbers separated by space: ").split()
print(f"You entered: {numbers}")
Comma-separated Input
values = input("Enter comma-separated values: ").split(',')
print(f"You entered: {values}")
No questions available.