⬅ Previous Topic
Python id() Function – Get Memory Address of an ObjectNext Topic ⮕
Python int() Function – Convert to Integer⬅ Previous Topic
Python id() Function – Get Memory Address of an ObjectNext Topic ⮕
Python int() Function – Convert to Integerinput()
FunctionThe input() function is used to read user input from the keyboard. It's one of the first functions you'll use when making interactive Python programs.
input(prompt)
prompt
(optional) – A string, shown to the user before input is read.string
.name = input("What is your name? ")
print("Hello, " + name)
What is your name? Alice
Hello, Alice
age = input("Enter your age: ")
print("You are", age, "years old")
Enter your age: 25
You are 25 years old
Even if you type numbers, input()
returns them as strings. You need to convert them manually using int()
or float()
.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum is:", num1 + num2)
Enter first number: 5
Enter second number: 10
Sum is: 15
int()
or float()
input()
In Python coding interviews, input is often simulated using input()
. Always remember that it returns strings and type conversion may be required.
input()
pauses the program and waits for user inputstring
int()
, float()
, or eval()
to convert the input if neededWrite a program that asks the user for two numbers and prints the multiplication result.
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Product is:", a * b)
⬅ Previous Topic
Python id() Function – Get Memory Address of an ObjectNext Topic ⮕
Python int() Function – Convert to IntegerYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.