Input and Output in Python - Interview Questions and Answers
You can use the input()
function to take user input.
name = input("Enter your name: ")
print("Hello,", name)
The input()
function always returns data as a string
age = input("Enter your age: ")
print(type(age)) # Output: <class 'str'>
Convert the input using int()
num = int(input("Enter a number: "))
print(num * 2) # Doubles the input number
Use split()
x, y = input("Enter two numbers: ").split()
print("X:", x, "Y:", y)
Convert inputs to integers using map()
x, y = map(int, input("Enter two numbers: ").split())
print(x + y)
Use list comprehension
numbers = [int(x) for x in input("Enter numbers: ").split()]
print(numbers)
Use try-except
try:
num = int(input("Enter an integer: "))
print(num)
except ValueError:
print("Invalid input! Please enter an integer.")
Use a loop
lines = []
while True:
line = input()
if line == "": # Stop on empty input
break
lines.append(line)
print(lines)
se sys.stdin.read()
import sys
data = sys.stdin.read()
print(data)
Use sys.argv
import sys
print("Arguments:", sys.argv[1:])
Use print()
print("Hello, World!")
Separate values with commas
print("Hello", "Python", 2025)
Use f-strings (Python 3.6+).
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Use end=""
print("Hello", end=" ")
print("World!")
Use *
operator.
print("Python " * 3) # Output: Python Python Python
Use sep
parameter
print("Python", "Java", "C++", sep=" | ")
Use the file
argument in print()
with open("output.txt", "w") as f:
print("Hello, file!", file=f)
Use str.ljust()
, str.rjust()
, or str.center()
print("Hello".ljust(10), "World".rjust(10))
Use zfill()
num = 42
print(str(num).zfill(5)) # Output: 00042
Use f-strings or format()
.
pi = 3.14159
print(f"Pi rounded: {pi:.2f}") # Output: Pi rounded: 3.14
In Python 2, print
is a statement (print "Hello"
). In Python 3, print()
is a function.
Use end=""
.
print("Hello", end="")
Use Unicode escape sequences.
print("\u2764") #
Use triple quotes.
text = """This is
a multi-line
input"""
print(text)
It waits for user input but does not display a prompt.
The user is prompted first, then the result is printed.
print("You entered:", input("Enter something: "))
Yes
def get_name():
return input("Enter your name: ")
name = get_name()
print("Hello,", name)
sys.stdin.read()
reads the whole input at once, while input()
reads one line.
Use open()
to read from a file.
with open("input.txt") as f:
data = f.read()
print(data)
Use ANSI escape codes or libraries like colorama
.
print("\033[31mHello in Red\033[0m")
Tutorials
Random Blogs
- Avoiding the Beginner’s Trap: Key Python Fundamentals You Shouldn't Skip
- Extract RGB Color From a Image Using CV2
- Variable Assignment in Python
- Best Platform to Learn Digital Marketing in Free
- The Ultimate Guide to Data Science: Everything You Need to Know
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- 5 Ways Use Jupyter Notebook Online Free of Cost
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Datasets for analyze in Tableau
- Loan Default Prediction Project Using Machine Learning