Different Input and Output Techniques in Python3
Last Updated :
10 Nov, 2022
An article describing basic Input and output techniques that we use while coding in python.
Input Techniques
1. Taking input using input() function -> this function by default takes string as input.
Example:
Python3
#For string
str = input()
# For integers
n = int(input())
# For floating or decimal numbers
n = float(input())
2. Taking Multiple Inputs: Multiple inputs in Python can be taken with the help of the map() and split() methods. The split() method splits the space-separated inputs and returns an iterable whereas when this function is used with the map() function it can convert the inputs to float and int accordingly.
Example:
Python3
# For Strings
x, y = input().split()
# For integers and floating point
# numbers
m, n = map(int, input().split())
m, n = map(float, input().split())
3. Taking input as a list or tuple: For this, the split() and map() functions can be used. As these functions return an iterable we can convert the given iterable to the list, tuple, or set accordingly.
Example:
Python3
# For Input - 4 5 6 1 56 21
# (Space separated inputs)
n = list(map(int, input().split()))
print(n)
Output:
[4, 5, 6, 1, 56, 21]
4. Taking Fixed and variable numbers of input:
Python3
# Input: geeksforgeeks 2 0 2 0
str, *lst = input().split()
lst = list(map(int, lst))
print(str, lst)
Output:
geeksforgeeks [2, 0, 2, 0]
Output Techniques
1. Output on a different line: print() method is used in python for printing to the console.
Example:
Python3
lst = ['geeks', 'for', 'geeks']
for i in lst:
print(i)
Output:
geeks
for
geeks
2. Output on the same line: end parameter in Python can be used to print on the same line.
Example 1:
Python3
lst = ['geeks', 'for', 'geeks']
for i in lst:
print(i, end='')
Output:
geeksforgeeks
Example 2: Printing with space.
Python3
lst = ['geeks', 'for', 'geeks']
for i in lst:
print(i,end=' ')
Output:
geeks for geeks
3. Output Formatting: If you want to format your output then you can do it with {} and format() function. {} is a placeholder for a variable that is provided in the format() like we have %d in C programming.
Example:
Python3
print('I love {}'.format('geeksforgeeks.'))
print("I love {0} {1}".format('Python', 'programming.')
Output:
I love geeksforgeeks.
I love Python programming.
Note: For Formatting the integers or floating numbers the original method can be used in the {}. like '{%5.2f}' or with the numbers, we can write it as '{0:5.2f}'. We can also use the string module '%' operator to format our output.
4. Output using f strings:
Python3
val = "Hello"
print(f"{val} World!!!")
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Python basic I/O Techniques
Taking input in Python Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input ()
3 min read
Python input() Function Python input() function is used to take user input. By default, it returns the user input in form of a string.input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input messageEx: input("What is your name? ")Returns: Return a string value as input by the user.By de
4 min read
Taking input from console in Python What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks
2 min read
Python - Print Output using print() function Python print() function prints the message to the screen or any other standard output device. In this article, we will cover about print() function in Python as well as it's various operations.Python# print() function example print("GeeksforGeeks") a = [1, 2, 'gfg'] print(a) print() Function Syntax
4 min read
Python - Output Formatting In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable. Python provides several ways to format strings effectively, ranging from old-style formatting to the newer f-string approach.Formatting Out
5 min read
Python3 I/O
Different Input and Output Techniques in Python3 An article describing basic Input and output techniques that we use while coding in python. Input Techniques 1. Taking input using input() function -> this function by default takes string as input. Example: Python3 #For string str = input() # For integers n = int(input()) # For floating or deci
3 min read