input()
input() is use when we need to enter data (when running the programme) on console and assign to a variable.
Usage
input.py
py
name = input("Please enter your name: ")
age = input("Please enter your age: ")
print(name)
print(age)
print(type(name))
print(type(age))Input Value:
John
56Results:
Please enter your name: John
Please enter your age: 56
John
56
<class 'str'>
<class 'str'>Note
- The
default data type from input()isstr. - So, we have to Convert the data type accordingly.
convert-input-data-type.py
py
age = input("Please enter your age: ")
age = int(age)
print(f"Value: {age} Type: {type(age)}")
# or
age2 = int(input("Please enter your age again: "))
print(f"Value: {age2} Type: {type(age2)}")Input value
56
78Results
Please enter your age: 56
Value: 56 Type: <class 'int'>
Please enter your age again: 78
Value: 78 Type: <class 'int'>