Console Input & Output in Python
A beginner-friendly Python tutorial on how to print output in the terminal and take user input (text + numbers) using print(), sys.stdout, sys.argv, and input().
Console Output Basics
Console output means showing text in the terminal. Python provides a built-in function called `print()` to display messages and values. It is simple, powerful, and used everywhere — especially for debugging and CLI programs.
1. print() (Normal Output)
Use print() to display text. It automatically moves to the next line after printing.
2. Printing Variables (f-strings)
Use f-strings to insert variables inside strings easily.
3. Printing Without Automatic Newline
Use the `end` parameter of print() to control newline behavior.
Printing Using sys.stdout
For more control (like CLI tools), you can use `sys.stdout.write()` which does not automatically add a newline.
1. sys.stdout.write()
This prints exactly what you provide — without adding a newline.
Formatted Console Output
Python provides methods like `ljust()` and `rjust()` to align text. This helps create clean CLI tables.
1. ljust() and rjust() Basics
`ljust(width)` aligns text left. `rjust(width)` aligns text right.
2. Print a Simple Table
Create a clean aligned table in terminal.
Console Input (Reading User Data)
Python allows reading user input in two main ways: 1) sys.argv (command-line arguments) 2) input() (interactive input)
1. sys.argv (Command-Line Arguments)
Example: user runs `python app.py John 22`.
2. input() (Interactive Input)
Program asks a question and waits for user input.
3. Scan a Number (Single Number Input)
Convert string input to number and validate.
4. Scan Multiple Numbers (Space Separated)
User enters numbers like: 10 20 30