Computer >> Computer tutorials >  >> Programming >> Python

Taking input in Python


In this tutorial, we are going to learn how to take input in Python.

In Python2, we will find two different functions to take input from the user. One is raw_input another one is input.

  • The function raw_input([promt]) is used to take the string as input from the user.
  • The function input([prompt]) is used to take the integers as input from the user.

Example

# taking 'string' input
a = raw_input('Enter your name:- ')
# printing the type
print(type(a))
# taking the 'int' input
b = input()
# printing the type
print(type(b))

Output

If you run the above code, then you will get the following result.

Enter your name:- Tutorialspoint
<type 'str'>
5
<type 'int'>

In Python3, the function raw_input() is removed. Now, we have only input([prompt]) function take the input from the user. And anything that user enters will be a string in Python.

We have to convert it into respective data types using different built-in functions. Let's see examples.

Example

# taking input from the user
a = input('Enter a name:- ')
# printing the data
print(type(a), a)
# asking number from the user
b = input('Enter a number:- ')
# converting the 'string' to 'int'
b = int(b)
# printing the data
print(type(b), b)

Output

If you run the above code, then you will get the following result.

Enter a name:- Tutorialspoint
<class 'str'> Tutorialspoint
Enter a number:- 5
<class 'int'> 5

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.