
In every programming language, the ability to interact with users is essential. Programs become meaningful only when they can accept input, process it, and display useful output.
What Is Input and Output?
Input refers to the data that a program receives from the outside world. This data can come from:
- The keyboard
- Files
- Databases
- Network sources
Output refers to the information that a program sends back to the user, usually displayed on the screen or written to a file.
Python primarily uses two built-in functions for this purpose:
input()→ to read data from the userprint()→ to display data on the screen
print() function
The print() function is a built-in tool used to display information to the console or other stdio streams. It converts any object passed to it into a string before displaying it
Basic Syntax
print(value1, value2, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Parameters
value(s): One or more objects of any type (strings, integers, lists, etc.) to be printed.sep(Optional): A string that specifies how to separate multiple objects. The default is a single space (' ').end(Optional): A string that is printed at the very end. The default is a newline character ('\n'), which moves the cursor to the next line.file(Optional): An object with a.write(string)method where the output is sent. The default issys.stdout(the screen).flush(Optional): A boolean that, ifTrue, forcibly flushes the output buffer to ensure the message appears immediately on the screen.
Examples
print("Hello, World!")
#Multiple args
print("User:", "Alice", "Age:", 30) # Output: User: Alice Age: 30
#Custom Separator
print("2025", "12", "31", sep="-") # Output: 2025-12-31
#Prevents New Line
print("Loading...", end="")
print(" Done!") # Output: Loading... Done!
input() Function
The input() function is a built-in tool used to capture data from a user at runtime. When called, the program pauses execution, waits for the user to type something and press Enter, and then continues.
Basic Syntax
variable = input("Optional message to display: ")
Examples
#Input will always be a string, so you would need to do something called #type conversion
age = int(input("Enter your age: ")) # Converts string input to an integer
price = float(input("Enter price: ")) # Converts string input to a float
Multiple Inputs
Multiple input Values for String data type
a, b = input("Enter two values: ").split()
Multiple input Values for Types other than string data type
x, y = map(int, input("Enter two numbers: ").split())
Pingback: Python Control Flow Statement– A Beginner’s Guide -