Variables, Expressions and Statements

Taking Input and Displaying Output in Python

The need of  taking input and displaying output arises for developers to interact with users to either provide a result of some sort or to get data. However, the main question is- how to take input in python? For reading input from the keyboard, python provides us with two inbuilt functions- input ( prompt ) and raw_input ( prompt ).

taking input and displaying output

                                                                                                                   taking input and displaying output 

What is Input ( ) 

When it comes to taking input, this function first takes the input from the user and then facilitates the evaluation of the expression. Furthermore, this means that an automatic identification takes place by python, one of the computer languages, that whether the user has entered a string or a number or list. Moreover, if the input provided is not correct, then python raises either syntax error or exception.

For example:

                    

# Python program showing

# a use of input()

val = input("Enter your value: ")

print(val)

Output:

                    

Enter your value: 123

123

>>>

After taking input, one can use the print() function to output data to the standard output device (screen). Furthermore, one can also output data to a file.

Browse more Topics Under Variables, Expressions and Statements

Working of Input Function in Python

With regards to taking input, the working of the input function in python is explained below.

  • When the execution of the input() function takes place, the program flow will be stopped until the input is provided by the user.
  • There would be a display of text or message on the output screen to ask a user to enter input value, i.e. there will be the printing of the prompt on the screen. Moreover, this is optional.
  • Whatever you enter as input, conversion of input function takes place into a string. Furthermore, if one enters an integer value, there will be the conversion of the still input() function into a string. Moreover, one will need to explicitly convert it into an integer in the code by making use of 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))

What is Raw Input

With regards to taking input, the working of this function takes place in the older version (like Python 2.x). Moreover, this function takes exactly what is typed by using the keyboard and converts it to a string. Afterwards, it returns it to the variable where one desires to store.

For example:

                    

# Python program showing

# a use of raw_input()

g = raw_input("Enter your name : ")

print g

Output:

Enter your name : geeksforgeeks

geeksforgeeks

>>>

Here, g is a variable that will get the string value whose typing takes place by the user during the execution of the program. Furthermore, the termination of typing of data for the raw_input() function takes place by the enter key. Moreover, one can use raw_input() to enter numeric data also, in which case, the use of typecasting takes place.

FAQs For Input and Displaying Output in Python

Question 1: How is the output produced in python?

Answer 1: The simplest way to produce output in python is by making use of the print() function where one can pass zero or more expressions that are separated by commas. This function, before writing to the screen, ensures the conversion of the expressions one passes into a string. Most noteworthy, it returns output to the screen.

Question 2: What is meant by a function in python?

Answer 2: A function refers to a block of organized, reusable code that one may use to perform a single, related action. Moreover, Python provides many built-in functions that are useful when taking input.

One can read python user input from the keyboard by making use of the input() built-in function. This provides an answer to the question- how to add user input in python. Also, people can create their own functions, known as user-defined functions.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

One response to “Values, Variables and Keywords”

  1. Brij Bhushan says:

    Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I also love your website because all type of information is available in your blogs. You made my day. Thanks you for everything. I have bookmarked more article from this website. Such a nice blog you are providing.

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.