Taking input in Python from Keyboard

Designers regularly have a need to cooperate with clients, either to get information or to give a type of result. Most projects today utilize an exchange box as a method of requesting that the client give some kind of information. While Python gives us two inbuilt capacities to peruse the contribution from the console.

  • input ( brief )
  • raw_input ( brief )

input ( ) : This capacity first takes the contribution from the client and afterward assesses the articulation, which implies Python naturally distinguishes whether client entered a string or a number or rundown. In the event that the info gave isn’t right then either language structure blunder or exemption is raised by python. For instance –

# Python program showing 
# a use of input()
 
val = input("Enter your value: ")
print(val)

Output:

Enter Your No: 123

Python User Input from Keyboard – input() work

  • Python client input from the console can be perused utilizing the info() worked in work.
  • The contribution from the client is perused as a string and can be relegated to a variable.
  • Subsequent to entering the worth from the console, we need to press the “Enter” button. Then, at that point, the info() work peruses the worth entered by the client.
  • The program stops endlessly for the client input. There is no choice to give break esteem.
  • In the event that we enter EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), EOFError is raised and the program is ended.

How the information work functions in Python:

At the point when input() work executes program stream will be halted until the client has given an information.
The message or message show on the result screen to request that a client enter input esteem is discretionary for example the brief, will be imprinted on the screen is discretionary.

Whatever you enter as info, input work convert it into a string. in the event that you enter a number worth actually input() work convert it into a string. You really want to unequivocally change over it into a number in your code utilizing pigeonholing.

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

Output :

Enter 123

raw_input ( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store. For example –

# Python program showing 
# a use of raw_input()
 
g = raw_input("Enter your name : ")
print g

Output :

Enter Your Name eevibes

Here, g is a variable which will get the string esteem, composed by client during the execution of program. Composing of information for the raw_input() work is ended by enter key. We can utilize raw_input() to enter numeric information too. All things considered we use typecasting.For more subtleties on pigeonholing allude this.

Also ReadHow to Install Chrome in Ubuntu?

Leave a Reply

Your email address will not be published. Required fields are marked *