How To Use Input Functions In Python Programming Language

In this lesson, you will learn a very interesting feature that is how to use input functions in Python programming language.

Just like any other high-level programming language Python has a simple and easy to read and understand syntax, it takes less execution time in terms of write and read the instruction.

And most importantly it is portable means you can run Python code on multiple OS’s. Programmers most of the time have to interact with the users for taking information or to tell the users about something. A dialog box is generally used to prompt a message to the user for asking specific information or for doing some kind of task. But in Python programming language it has two inbuilt functions that help to take the input from a user and most of the time it is through the keyboard.

Kinds Of Python Input Function

Python provides the following two inbuilt function to take input from the user.

  • raw_input ( Your message / variable )
  • input ( Your message / variable )

Now let’s talk about each of the above Python input functions.

raw_input (): As you know before the release of Python 3 there was Python 2 and this functions works in Python 2.0.  The main reason to use this function is to collect information types by the user through the keyboard. This function takes the information, converts it into a string and after it returns the result into a variable to store this information. See the example below:

# This function shows user giving input 
# Use use " x " as a variable to store the information

x = raw_input("Please Enter Your Name : ") 
print x 

Output results:

Please Enter Your Name: WalkIntoPC
WalkIntoPC

>>>

As you can see the function works just fine. In the above example, we use ” x ” as a variable to store information in it provides by the user. In our case, the information is ” WalkIntoPC “.

Recommended:  Loops And How To Use Them In Python Programming Language

input(): This second function came up with the release of Python 3.0. The advantage of this function is it first takes the input provided by the user and then analyze the expression. By expression analysis, I mean that Python automatically decides whether the input is a string or an integer number.  If in any case user provides the wrong input Python shows the error or just shows an exception message to the screen.  See the example below

# This input function asking the user the enter a number

value = input("Please Enter Your Number: ") 
print(value) 

Output results:

Please Enter Your Value: 2019
2019
>>>

Now we will break down the steps that this Python input function takes to complete this execution.

  1. First thing first, when the input() function executes the program continuation stops until the user has given any input.
  2. Our text message shows/displays on the screen to tell the user to enter the input value.
  3. Now if the user enters the input this function first converts that information into string no matter even if you typed the integer value it always convert it into string unless you explicitly tell the function to convert the input into an integer by using the typecasting.
Recommended:  Basic Operators In Python Programming Language

See the example below:

#In this we use typecasting to check the data type of user input

number = input ("Please Enter Your Number :") 
print(number) 
name = input("Please Enter Your Name : ") 
print(name) 
 
print ("type of number", type(Number)) 
print ("type of name", type(name)) 

Output results:

Please Enter Your Number: 2019

2019

Please Enter Your Name: WalkIntoPC

WalkIntoPC

type of number <class 'str'>

type of name <class 'str'>

What About Showing A Helpful Hint During The Display Of Message For The User

It is a good programming practice to guide the user that what input should be given here. This can be done by putting the hint message in double-quotes and print it to the next line of input. When the function runs on the first line a prompt for user input and on the second line your hint displays or you can display your hint inside the input field. See the example below.

    number = input("Plese Enter Your Desired Number: ")
    Plese Enter Your Desired Number: 7
    print(number )
    7

We have given the hint to the user that a number should be entered as an input. On the next line, this message prompts to ask the user to enter an integer number of his own desire, as the user enters the number the variable that we use the name as ” number ” stores that information and print () function display it on the screen.

Recommended:  First Program Hello World In Python Programming Language

Jack Sparrow

Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Leave a Reply

Close Menu