1.

How to take input in python

Answer»

>

Taking input in Python

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with TWO inbuilt functions to read the input from the keyboard.

input ( prompt )

raw_input ( prompt )

input ( ) : This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a STRING or a number or list. If the input provided is not correct then either syntax error or exception is raised by python. For example –

# Python program showing 

# a use of input()

  

val = input("ENTER your value: ")

print(val)

Output:



 

How the input function works in Python :

When input() function executes program flow will be stopped until the user has given an input.

The text or message display on the output SCREEN to ASK a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional.

Whatever you enter as input, input function convert it into a string. if you enter an integer value still input() function convert it into a string. You need to explicitly convert it into an integer in your code using typecasting.

Code:

# Program to check input 

# type in Python

  

num = input ("Enter number :")

print(num)

name1 = input("Enter name : ")

print(name1)

  

# Printing type of input value

print ("type of number", type(num))

print ("type of name", type(name1))



Discussion

No Comment Found