input() Function
The input() function is used to take input from the user.
The text inside input() is called the prompt. It is displayed to the user to tell them what information to enter.
The input() function is used to take input from the user.
The text inside input() is called the prompt. It is displayed to the user to tell them what information to enter.
Here "Enter your name: " and "Enter your age: " are prompts
So if user enters Alice and 20 respectively.
Output:
-----
Enter your name: Alice
Enter your age: 20
My name is Alice. I am 20 years old.
Let's print the type of inputs
Output:
-----
Enter your name: Alice
Enter your age: 20
<class 'str'>
<class 'str'>
Important: Whatever the user enters using input() is returned as a string by default, even if the user enters a number.
name = input("Enter your name: ")
age = input("Enter your age: ")
print("My name is " + name + ". I am " + age + " years old.")
So here we can skip type conversion for age.
Output:
-----
Enter your name: Alice
Enter your age: 20
My name is Alice. I am 20 years old.
But, If we want to perform meaningful operations on the input, we may need to convert it to the appropriate data type.
We can also perform the type conversion while taking the input.
age = int(input("Enter your age: "))
average_marks = float(input("Enter your average marks: "))