Python Input and Output

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Input Function in Python

Overview

There are several ways to deliver the output of a program. In Python, we use the print() function to output data to the screen. Sometimes we might want to take input from the user. We can do so by using the input() function. Python takes all the input as a string input by default. To convert it to any other data type, we have to convert the input explicitly.

Scope of article

  • This topic will introduce us to two inbuilt functions in Python i.e Python input() & Python print().
  • We will learn various available arguments in print() and control the output format.
  • Then, we will also learn the syntax of Python input() along with examples.

Introduction

The main objective of a programming language is to make it interactive with the user and the audience. If you notice precisely, most of the software we use in real life needs input from a user, whether a search engine or a daily used app or even mail where we first input our mail id and password.

In Python, we have many inbuilt functions that help obtain data from the user and then display the data after processing some particular logic in the code.

You can refer to the below flowchart to clarify how the system of inputting data and displaying it on some devices works.

input and output in python This article will go through different ways of printing data and taking inputs, with some examples. Let’s learn about displaying the data (output) before diving into inputting any data from the user.

Python Output

We use the widely used print() statement to display some data on the screen.

We can output the particular data on some device(screen) or even in some file.

While writing real-world programs, we have to write statements that explicitly output numbers and strings.

Let’s take an example to display a simple text.

Output:

Don’t worry much about double or single quotes. Either of them will work fine in python.

Before learning more about output in python, let’s understand the actual syntax of the print() function. This function takes some additional arguments other than objects/values that offer more control over the output format.

For your better understanding of the syntax here we have defined a few keywords of the above statement:

  • object(s) are the values to be printed on the screen. They are converted to strings before getting printed.
  • sep keyword is used to specify how to separate the objects inside the same print statement. By default, we have it as sep=' ', a space between two objects.
  • end is used to print a particular thing after all the values are printed. By default, we have end as \n, which provides a new line after each print() statement.
  • file is used to specify where to display the output. By default, it is sys.stdout (which is the screen).
  • flush specifies the boolean expression if the output is False or True. By default, it is False. In Python, the output from the print() goes into a buffer. Using the flush= True parameter helps in flushing the buffer as soon as we use the print() statement.

Let’s take some examples to understand using the print() function’s syntax.

Example 1:

Output:

Note: All the values are followed by a comma( , ), distinguishing between themselves as separate values/objects. By default, this adds a space between the values printed as output.

Example 2:

We can even perform arithmetic operations directly in the print statement in python.

Output:

Example 3:

Output:

In the above code snippet, both words were printed in the same line because the end by default wasn’t ‘\n’, instead was ' ' (space).

Example 4: We can print a string and number together.

Output:

While printing numbers and strings together, we need to convert the number into a string to concatenate. The str() is another inbuilt function that converts an integer into a string.

Python Output Formatting

Sometimes a user might want to make the output of a code prettier and more attractive to the audience. For this, we have some other inbuilt functions. Here we will discuss the two widely used methods.

We can use the str.format() method. This format() function will make the output more presentable.

The following curly braces { } in the code acts as placeholders. We can also specify the order of these variables by putting numbers in the placeholders.

Output:

Besides this function, we can also use the old % method which we used similarly in C language.

Output:

Note: Though other methods are available for formatting, the string modulo operator is still widely used by users.

Python Input

Till now, we learned about how to display the python output. The programs were not too interactive as we had hard-coded values of the variables. Sometimes users or developers want to run the programs with their own data in the variables for their own ease.

To execute this we will learn to take input from the users, in which the users themselves will define the values of the variables.

The input() statement allows us to do such things in Python.

The syntax for this function is as follows:

Here prompt is the string we want to display while taking input from the user. This is optional.

Let’s look at some examples to have more clarity on this topic.

Example 1:

Output:

Example 2:

Output:

Note: By default, the python input() function takes the user’s input as a string. In example 2, we had to convert the age (from string to integer), which was inputted by the user, using the int() along with the input function.

We again need to convert the integer (‘new’ variable) into a string for concatenation during printing the output.

Example 3:

Output:

In the print() statement we have mentioned “%.2f”. This “.2” specifies that the answer that is to be printed should be up to 2 decimal places.

Note: Instead of int(), we can also use the float() function, allowing users to enter decimal numbers instead of integers.

Conclusion

Learned a lot of methods and ways of taking data and displaying it. Don’t worry; go through the steps and examples of each variation to clear your concept well. All the concepts are aided by simple and easy-to-understand code snippets. This article elaborately describes the process of Input and Output in Python.

Python syntax is quite short and precise. While going through this blog, you might have noticed that compared to the other programming languages such as C++ and Java, which needs a header file to start. Here in Python, it takes just a single line for inputting and displaying data. No such header files are required.

Try using such different methods to improve your coding experience in Python language and practice similar problems to understand the logic well.

I hope this blog solved most of your doubts and was informative.

Read More:

  1. Python Modules
  2. Format in Python
  3. String Formatting in Python
  4. int() Function in Python