Input and Output Functions in Python
Input and Output (I/O) functions play a vital role in Python programming. The fundamental
purpose of any computer program is to interact with the user by accepting data, processing
it, and returning a meaningful result. In Python, this two-way communication is handled
smoothly using the input() and print() functions.
There is a core concept in programming known as GIGO (Garbage In, Garbage Out). This
means your output will be accurate only if your input is correct. If the user provides wrong
data, the program will inevitably generate an incorrect output.
1. The Output Function: print()
The print() function is the most basic and frequently used built-in function in Python. Its primary purpose is to display text, variables, or the results of calculations on the output window or console screen. It evaluates the expressions you pass to it and converts them into a human-readable string format before displaying them. Example :
print(10)
10
In this example, passing the integer 10 inside the print() function directly displays 10 on the screen.
2. The Input Function: input()
The data provided by a user to the program is called input. This data can belong to any data type, such as integers (int), decimal numbers (float), or text (string). Python provides the built-in input() function to capture user data at runtime.Example:
name = input("Enter your name = > ")
Enter your name = > shubham
In the statement above, name acts as a variable, which serves as a storage location in the computer's memory. When this line runs, the program pauses and displays the message "Enter your name => ". Once the user types a value (for example, shubham) and presses Enter, the input() function captures that string and stores it safely inside the name variable for later use.
Features Of Input / Output Functions
1. Dynamic Text Prompt : The input() function not only takes input, but also
allows displaying a customized message (prompt) (e.g., input("Enter your age: ")). This lets the
user know what to type.
2. Default String Input Feature : The most important feature of input() is that
whether the user types a number or a letter from the keyboard, it always captures it as a string
(String/Text).
3. Automatic Newline Block : The print() function has an in-built feature that
automatically moves the cursor to the next line (\n) after completing its task. Its behavior can
also be changed with the end parameter.
4. Multi-Object Printing : The print() function can print multiple data types
(e.g., string, number, list) simultaneously in a single line separated by a comma (,).