Basic Input/Output in Python

This post is lesson 8 of 54 in the subject Python Programming Language

This document provides an overview of basic input/output operations in Python. It includes examples of how to use the input() function to gather user input from the keyboard, and how to use the print() function to output data to the screen. The document also covers how to format output with the str.format() function and the % operator. This document also provides examples of how to convert user input to various data types and how to input multiple values from a user.

1. Input in Python

Python provides the input() function to help us enter the necessary data from the keyboard for the program. Example:

val = input("Enter your value: ")
print(val)
num = input ("Enter number: ")
print(num)
name1 = input("Enter name: ")
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))

Output

Enter your value: 5
5
Enter number: 9
9
Enter name: gochocit.com
gochocit.com
type of val is  <class 'str'>
type of num is  <class 'str'>
type of name1 is  <class 'str'>

When the input() function executes, the program flow will stop until the user enters something.

Note: Whatever we enter, the input() function will convert it to a string. If we enter an integer, the input() function still converts it to a string. Therefore, we can convert it to a numeric data type with the built-in functions in Python.

Example:

val = input("Enter your value:")
num = input ("Enter number:")
name1 = input("Enter name:")

# Convert data type
val = int(val)
print(val)
num = float(num)
print(num)
print(name1)

# Printing type of input value
print ("type of val is ", type(val))
print ("type of num is ", type(num))
print ("type of name1 is ", type(name1))

Output

Enter your value:5
Enter number:9
Enter name:gochocit.com
5
9.0
gochocit.com
type of val is  <class 'int'>
type of num is  <class 'float'>
type of name1 is  <class 'str'>

We can also convert input() to integerfloat, and string as follows:

# input
print("input integer:")
num1 = int(input())
num2 = int(input())

# printing the sum in integer
print("sum of num1 and num2 = ", num1 + num2)

# input
print("input float:")
num1 = float(input())
num2 = float(input())

# printing the sum in float
print("sum of num1 and num2 = ", num1 + num2)

# input
print("input string:")
string = str(input())

# output
print(string)

Output

input integer:
5
2
sum of num1 and num2 =  7
input float:
2.1
5.0
sum of num1 and num2 =  7.1
input string:
gochocit.com
gochocit.com

Entering multiple values from the user in Python

Using the split() function

This function helps to get many values entered by the user, which are separated by a specified separator. By default, the separator is set to a space. Syntax:

input().split(separator, maxsplit)

The separator is what separates the values. By default, it is a space. The maxsplit parameter determines the maximum number of values that the user can input.

Example:

# taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)

# taking two inputs at a time with comma (,)
x, y = input("Enter two values: ").split(",")
print("Number of boys: ", x)
print("Number of girls: ", y)

# taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)

# taking two inputs at a time
a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))

# taking multiple inputs at a time
# and type casting using list() function
x = list(map(int, input("Enter multiple values: ").split()))
print("List of numbers: ", x)

Output

Enter two values: 5 9
Number of boys:  5
Number of girls:  9
Enter two values: 5,9
Number of boys:  5
Number of girls:  9
Enter three values: 1 7 3
Total number of students:  1
Number of boys is :  7
Number of girls is :  3
Enter two values: 9 8
First number is 9 and second number is 8
Enter multiple values: 7 8 1 2 3
List of numbers:  [7, 8, 1, 2, 3]

Using list comprehension

This is a simple way to create a list in Python. However, we can also use it to enter multiple values into the program from the user.

# taking two input at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)

# taking three input at a time
x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)

# taking two inputs at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))

# taking multiple inputs at a time
x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x)

Output

Enter two values: 5 9
First Number is:  5
Second Number is:  9
Enter three values: 5 7 9
First Number is:  5
Second Number is:  7
Third Number is:  9
Enter two values: 1 2
First number is 1 and second number is 2
Enter multiple values: 1 5 7 8 9
Number of list is:  [1, 5, 7, 8, 9]

2. Output in Python

Python supports the print() function to print data to the screen. Syntax:

print(value(s), sep= ' ', end = '\n', file= sys.stdout, flush=False)

Where:

  • value(s): values that will be converted to a string to print to the screen.
  • sep=’ ’: optional parameter, may or may not be. Moreover, specifies the separator between values to be printed in case there are many values. sep defaults to ’ ‘.
  • end=’\n’: optional parameter, may or may not be. In addition, specifies the character printed last. end defaults to ‘\n’.
  • file: optional parameter, may or may not be. Specifies the object that receives the value to print. The default is sys.stdout (which is printed on the screen).
  • flush: optional parameter, may or may not be. If True, do not save the value in the buffer. If False, save the value in the buffer. The default is False.

Example:

print("Gochocit.com")
print("Welcome to \n Gochocit.com")
print("Hello!", end= "**")
print("Welcome to Gochocit.com")
b = "to"
print("Welcome", b, "Gochocit.com")
print("Welcome", b, "Gochocit.com", sep="-")
x = 5
print("x =", x)

Output

Gochocit.com
Welcome to
 Gochocit.com
Hello!**Welcome to Gochocit.com
Welcome to Gochocit.com
Welcome-to-Gochocit.com
x = 5

Output with format

We can format the output values with the str.format() function.

x = 5; y = 10
print('The value of x is {} and y is {}'.format(x,y))
print('I love {0} and {1}'.format('bread','butter'))
print('I love {1} and {0}'.format('bread','butter'))

Output

The value of x is 5 and y is 10
I love bread and butter
I love butter and bread

In the example above, curly braces {} are utilized to indicate the output values. Furthermore, the order of the values can also be specified with indexes.

We can use keyword arguments that we define ourselves to format strings.

print('Hello {name}, {greeting}'.format(greeting = 'Welcome to Gochocit.com', name = 'John'))

Output

Hello John, Welcome to Gochocit.com

Moreover, we can also format strings like C++ printf() function with the % operator.

x = 12.3456789
print('The value of x is %3.2f' %x)
print('The value of x is %3.4f' %x)
y = 5
name = "Gochocit.com"
print("num = %d" %y);
print("My name is %s" %name);

Output

The value of x is 12.35
The value of x is 12.3457
num = 5
My name is Gochocit.com

In this lesson, we have learned about basic input/output in Python. In the next lesson, we will learn about variable memory and memory management in Python.

5/5 - (1 vote)
Previous and next lesson in subject<< Types of operators in PythonVariable memory and memory management in Python >>

Leave a Reply

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