Python: I/O [Input/Output]

Use the input function to take input from user(in the terminal). The input() function will wait for the user to enter some data-

x = input() # This will wait for the user input

print(x)
print(type(x))

y = input() # This will wait for the user input

print(y)
print(type(y))
Python

Output:

If we check the type of data obtained from input() function, it is ‘str’. No matter what we enter, then input function will take it as string.

abc <- this is the input we entered

abc <- this is the outut printed by the print function
<class 'str'>


22 <- this is the input we entered

22 <- this is the outut printed by the print function
<class 'str'>
Python

We can provide some string(to ask user to input value), and the input() function will show the string first and wait for the user input.

name = input("Enter your name here: ") # It will wait for the user input

print(f"Your have entered: {name}")
Python

Output:

Enter your name here: bigboxcode

Your have entered: bigboxcode
Python

We can take as many input we want and save those in variables. Then we can use the variables later.

first_num = input("Enter first number: ")
second_num = input("Enter second number: ")

sum = int(first_num) + int(second_num)

print("Sum of the 2 numbers are: ", sum)
Python

We may need to type cast the value in some cases.

Output:

Enter first number: 38
Enter second number: 20

Sum of the 2 numbers are:  58
Python

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.