Python Input & Output Statements

Python Input & Output Interview Questions

What is the print() function in Python?
The print() function is used to output data to the standard output device (screen). It converts objects to strings and writes them to stdout. Syntax: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False).
What is the input() function in Python?
The input() function reads a line from input (usually keyboard) and returns it as a string. It can optionally take a prompt string as argument. Example: name = input("Enter your name: ").
What is the difference between print in Python 2 and Python 3?
In Python 2, print is a statement: print "Hello". In Python 3, print() is a function: print("Hello"). Python 3 requires parentheses.
How to format output using the print() function?
Multiple ways: Using comma separator, string concatenation, f-strings (Python 3.6+), format() method, or % formatting. Example: print(f"Value: {x}"), print("Value:", x).
What are f-strings in Python?
f-strings (formatted string literals) were introduced in Python 3.6. They allow embedding expressions inside string literals using curly braces {}. Example: name = "John"; print(f"Hello {name}") outputs "Hello John".
What is the purpose of sep and end parameters in print()?
sep specifies the separator between multiple objects (default is space). end specifies what to print at the end (default is newline \n). Example: print(1, 2, 3, sep="-", end="!") outputs "1-2-3!".
How to take multiple inputs in one line?
Use split() method with input(): a, b = input().split(). To convert to specific types: a, b = map(int, input().split()) or a, b, c = map(float, input().split()).
What is the difference between input() and raw_input()?
In Python 2, input() evaluates the input as Python code, while raw_input() returns a string. In Python 3, raw_input() was renamed to input(), and the old input() was removed.
How to handle EOFError with input()?
Use try-except block. EOFError occurs when input() hits end-of-file condition (Ctrl+D in Linux/Mac, Ctrl+Z+Enter in Windows).
try: data = input() except EOFError: print("End of input reached")
What is the format() method for string formatting?
The format() method formats specified values and inserts them into string placeholders {}. Example: "{} is {} years old".format("John", 25) or "{name} is {age} years old".format(name="John", age=25).
How to print without newline in Python?
Set end parameter to empty string or space: print("Hello", end="") or print("Hello", end=" "). In Python 2: print "Hello", (trailing comma).
What is % formatting in Python?
Old-style string formatting using % operator. Example: "Name: %s, Age: %d" % ("John", 25). Placeholders: %s (string), %d (integer), %f (float), %x (hexadecimal).
How to read input until EOF (End of File)?
Use a while loop with try-except or read from sys.stdin. Common patterns:
import sys for line in sys.stdin: # process line # OR while True: try: line = input() except EOFError: break
What is sys.stdout and sys.stdin?
sys.stdout is the standard output stream (screen). sys.stdin is the standard input stream (keyboard). They are file-like objects. The print() function writes to sys.stdout by default.
How to redirect output to a file?
Use the file parameter in print() or redirect sys.stdout.
# Method 1 with open("output.txt", "w") as f: print("Hello", file=f) # Method 2 import sys sys.stdout = open("output.txt", "w") print("Hello")
What is the eval() function with input()?
eval() evaluates a string as Python expression. With input(): x = eval(input()) allows entering expressions like "2+3" or "[1,2,3]". Warning: Can be a security risk with untrusted input.
How to print formatted tables or columns?
Use string formatting with field width:
# Using format() print("{:<10} {:<10} {:<10}".format("Name", "Age", "City")) print("{:<10} {:<10} {:<10}".format("John", "25", "NYC")) # Using f-strings name, age = "John", 25 print(f"{name:10} {age:10}")
< for left align, > for right align, ^ for center align.
What is the flush parameter in print()?
When flush=True, the output is forcibly written to the stream. Normally, output is buffered. Useful for real-time output like progress bars. Example: print("Loading...", end="", flush=True).
How to read password input without echo?
Use the getpass module:
import getpass password = getpass.getpass("Enter password: ")
This hides the input while typing.
What are the different ways to write to files in Python?
Using open() with write modes:
# Write text with open("file.txt", "w") as f: f.write("Hello\n") print("World", file=f) # Append text with open("file.txt", "a") as f: f.write("Appended text\n")
Modes: 'w' (write), 'a' (append), 'x' (exclusive creation).
Note: These questions cover Python's input/output operations. Python provides flexible I/O options from simple print() to advanced file handling. Remember that input() always returns a string, so type conversion is often needed. For secure applications, be cautious with eval() and consider using safer alternatives like ast.literal_eval().
Python Input & Output Statements Next