In Python, you can take input from the user using the input() function. The input() function reads a line of text entered by the user and returns it as a string.
Example: Taking a Single Input
python
name = input(“Enter your name: “)
print(“Hello, ” + name + “!”)
Example:
- Taking Integer Input
To take numerical input, you can convert the string to an integer (or other data types) using int():
python
age = int(input(“Enter your age: “))
print(“You are”, age, “years old.”) -
Taking Multiple Inputs
1. *Space-Separated Input:*
Use split() to separate inputs:
python
x, y = input(“Enter two numbers separated by space: “).split()
print(“You entered:”, x, “and”, y)
2. *Convert to Integers:*
python
x, y = map(int, input(“Enter two numbers separated by space: “).split())
print(“Sum:”, x + y)
- Taking List Input
If you want to take a list of numbers as input:
python
numbers = list(map(int, input(“Enter numbers separated by space: “).split()))
print(“List:”, numbers) -
Taking Input Until a Condition
To continuously take input until a specific condition is met:
python
while True:
data = input(“Enter something (type ‘exit’ to quit): “)
if data.lower() == ‘exit’:
break
print(“You entered:”, data)
These methods allow you to handle various input scenarios effectively!