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()

raw_input(): This 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.


input(): This 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.

Program Example

This program 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.


Program Example

See, in this program, user input is string type. We used type() method to check value type.

# 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'>

Program Example: Casting User input

See, in this program, we are converting user input into int type. 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