Javaexercise.com

How to get user input in Python?

To get input from the user in Python program, Python provides two methods.

  • raw_input()
  • input()

Python raw_input() Method

The raw_input() method is used to get user input but supported in the older versions of Python. We can use it with Python2.x version series. It is not supported in Python3 and later versions.


Python input() Method

The input() method is used to get user input but supported in the Python3 and latest versions of Python. We can use it with Python3.x version series. It is not supported in Python2 and earlier versions.

How To Take User Input in Python 3

This Code example is tested and executed using Atom IDE with Python 3.8 version.

# Getting user input
a = input("Enter a value")
print(a)
 Output:

Enter a value 12
12

Python input() method returns user input in string type. So if you want different type, use type casting.


How to check user input type in Python?

See, in this code example, user input is a string type. We used type() method to check value type in Python.

# Getting user input
a = input("Enter a value")
print(a)
# Checking user input type
print(type(a))
 Output:

Enter a value 12
12
< class 'str'>

Casting User input in Python3

See, in this code example, we are converting user input into int type by using int() method. This is called type casting, we will discuss it in detailed in type casting topic.

# Getting user input
a = input("Enter a value")
# Casting user input to int type
a = int(a)
print(a)
# Checking user input type
print(type(a))
 Output:

Enter a value 12
12
< class 'int'>


Take Float Input in Python