Tutorial

Basic Input Output Operations in Python

3 min read

In this article, we will learn about the basic input output operations in Python. There are different ways of using the input and output properties of the Python programming language which we are going to discuss here.

Input operations in Python

As programmers when we are writing our code, we sometimes require to provide some input to the computer. This can be done by using the input() function in Python.

Note: The input() function takes the input from the user and by default returns it as a string data type. We need to use a different type of conversion technique if we want to change the data type of our inputs.

String input in Python

Example of how to take input as a String in Python

name = input("Enter your name: ")
print("Hello, " + name)

Here, we ask the user to enter their name and then print a greeting using their name.

Input operation using Type Conversion

To read an integer from the user, we can use the int() function.

Example:

age1 = int(input("Enter age of father: "))
print(age1)
age2 = int(input("Enter age of son: "))
print(age2)
age_sum = age1 + age2
print("Combined age of father and son is " + str(age_sum) + " years.")

The output of this will be:
50
20
Combined age of father and son is 70 years.

Here, we ask the user to enter the age of the father and son, and then print it out.

We use the int() function to change the data type of their age variables so we can calculate their sum. Then we need to convert the age to a string using the str() function before we can concatenate it with the output strings.

Output operations in Python

While writing our code we need to show the output of certain actions implemented inside our code. We can do that using the print() function. The full syntax of the print() function is:

print(object= separator= end= file= flush=)

where,

  1. object – values that will be printed. This is a compulsory field.
  2. separator – allows us to separate multiple objects inside print().
  3. end – allows us to add specific values like new line “\n”, tab “\t”.
  4. file – where the values are printed.
  5. flush – boolean specifying if the output is flushed or buffered. The default value is False.

Basic Print statement in Python

Example of how to print a value in Python

print("Hello World!")

The output of this will be:
Hello World!

Here, the print() function only uses the object parameter.

Output in a single line

Print statement using end parameter in Python

Example:

print("Hello World", end=', ')
print("this is 2023.")

The output of this will be:
Hello World, this is 2023.

Here, we use the end parameter to print out two separate strings by ending the first string with a comma so that we get the output in a single line.

Print statement using separator parameter in Python

Example:

print("Hello World", "this is", "2023", sep='...')

The output to this will be:
Hello World...this is...2023

Here, we use the sep parameter to print out more than one object inside a print statement together in one line.

Print statement using Format String for Output Operations in Python

Example:

x = 5
y = 10
print(f'The value of x is {x} and y is {y}.')

The output of this will be:
The value of x is 5 and y is 10.

Here, we use the format string to directly insert the values inside the string during the print() operation. This is done using the curly braces( { } ) as placeholders. We can insert multiple values using this method.

Conclusion

In this article, we learned about the basic input and output operations in Python. We saw examples of how to use the input() function to read input from the user and the print() function to print output on the screen. We also saw how to use string formatting to perform more advanced output operations.