Online Tutorials & Training Materials | STechies.com
Register Login

How to take Input() in Python?

|| 0

How to take Input() in Python?
Stechies

In python there are two inbuilt function to take input from keyboard which are:

  • raw_input ( prompt )
  • input ( prompt ) 

Python takes input from the keyboard and store the input in a variable to perform such operations.

raw_input() function

This function works with the python 2.x and older version, it takes the input in the exact same manner as entered by the user and convert it to string then store them in the variable.

Example:

# Python program to explain raw_input() function

# Prompt user to enter value
myname = raw_input('What is your Name: ')

# Print entered a value on the screen
print('My name: ', myname)

# Print variable type
print(type(myname))

# Prompt user to enter value
age = raw_input('What is your Age: ')

# Print entered a value on the screen
print('My Age: ', age)

# Print variable type
print(type(myname))

Output:

What is your Name: ss
My name:  ss
<class 'str'>
What is your Age: 66
My Age:  66
<class 'int'>

input() function

Input() function prompt user to enter the value, it takes value from the user and convert them in string and save in variable for further use, Which means when you enter an integer value it will convert into a string. In this case we need to use typecasting in our code to explicitly convert it into an integer.

Example:

# Python program to explain input() function

# Prompt user to enter value
myname = input('What is your Name: ')

# Print entered value on screen
print('My name: ', myname)

# Print variable type 
print(type(myname))

# Prompt user to enter value
age = input('What is your Age: ')

# Print entered value on screen
print('My Age: ', age)

# Print variable type 
print(type(myname))

Output:

What is your Name: ss
My name:  ss
<class 'str'>
What is your Age: 66
My Age:  66
<class 'str'>