Perform Input And Output Operations - Python

Input Operation in Python

 
The Python programming language provides input() function to take input into a program from console.
 
input() 
 
The simplest way to take input is using the input() function which first takes the input from the user, then evaluates the expression and finally converts the entered value into the string format (irrespective of format or type of entered value). The input() method accepts a string message which is an optional and meant to be displayed on the output console to ask a user to enter input value. Let’s have a look at the syntax
  1. num = input ("Enter number:")   
  2. print(num)    
  3. # Printing type of input value   
  4. print ("type of number", type(num))  
Perform Input And Output Operations - Python
 
You will need to typecast the input to the desired type because the entered value is always returned as string.
 
split() 
 
Using split() method, we can take multiple values in one line. Split method breaks the given input by the specified separator. If separator is not provided, then any white space is a separator. Let’s have a look at the syntax and example to understand in a better way.
  1. # taking two inputs at a time   
  2. x, y = input("Enter a two value: ").split()   
  3. print("Number of boys: ", x)   
  4. print("Number of girls: ", y)   
  5. print()  
Perform Input And Output Operations - Python
 

Output Operation in Python

 
print()
 
Python programming language provides print() function to present the output of a program. The simplest way to present or display a program output to the console is using the print() function where you can pass zero or more expressions separated by commas. Let’s have a look at the example:
 
Example 1
 
Print two integers value 
  1. # printing two numbers at a time   
  2. a=7   
  3. b=9  
  4. print(a, b)  
Perform Input And Output Operations - Python
 
Example 2
 
Print string and integer value using print command 
  1. num = input ("Enter number :")   
  2. print('number entered is : ', num)  
Perform Input And Output Operations - Python
 
By default, python print() function use space ‘ ’ as separator between arguments passed in print() function but we can modify or use any character, number or string as separator. ‘sep’ (Optional) parameter in print() function can be used to specify the character or number or string as a separator to separate the arguments passed in print(). 
  1. print('Male''Female', sep='/') will print both Male and Female separated by ‘/’  
Perform Input And Output Operations - Python
 
Similarly, for formatting a date we can specify a dash ‘-’ as separator 
  1. #for formatting a date   
  2. print('09','12','2018', sep='-')   
Perform Input And Output Operations - Python
 
‘end’ (Optional) parameter in print() function can be used to specify the character or number or string to print at the end. Default is new line character ‘\n’. 
  1. # ends the output with a <space>    
  2. print("Welcome to" , end = ' ')    
  3. print("Python Progamming Course", end = ' '))   
Perform Input And Output Operations - Python
 
‘file’ (Optional) parameter in print() function can be used to specify where the function should write a given object(s) to. If not specified explicitly, it is sys.stdout by default.
  1. # Code for printing to a file   
  2. welcomeFile = open("c:\\welcome.txt"'w')     
  3. print('python course', file = welcomeFile)   
  4. welcomeFile.close()   
To verify the result, open the file and look for the entered content.
 
Till now we looked at the way to present the output of print the output using print() function but sometimes user often wants more control the formatting of output than simply printing. There are several ways to format output as mentioned below,
 

Formatting output using String modulo operator (%)

 
The String modulo operator (%) can be used for string formatting by adding a placeholder in the string which later replaced with the values in tuple. String modulo operator ( % ) can be used to replace any integer, float or string values. Let’s have a look at the example to understand better 
  1. # print integer and float value   
  2. print("Integer : % 2d, Float : % 5.2f" %(105.333))    
Use “%d” for integer value and “%f” for float value. In the above example “%2d” is used for the integer 1. The prefix ‘2” signifies that the number will be printed with 1 leading blank character as integer value 1 consists only of one digit. The second one “%5.2f” is a format description for a float number. the number following the “.” in our placeholder signifies the decimal part of the number or the precision, character “f” of our placeholder stands for “float”
 
Perform Input And Output Operations - Python
 

Formatting output using format method

 
use {} to mark where a variable will be substituted and can provide detailed formatting directives. We can either use the exact position of variable to be substituted or empty {} will substitute the variables in the order mentioned. Let’s have a look at the example where we have used a position of the object
  1. # using format() method and referring a position of the object   
  2. print('{0} {1} {2}'.format('Python''Programming''Course'))  
Perform Input And Output Operations - Python
 
In the above example, the brackets and characters within them (called format fields) are replaced with the objects passed into the format() method. A number in the brackets is used to refer to the position of the object passed into the format() method.
 

Conclusion

 
In this article we read about the different ways to take input and present output supported in Python programming language.