Python - Input Output
What are Input and Output?
Input allows users to send data to a Python program during execution.
Output displays data or results to the user.
Why are they important?
- Enable dynamic interaction with users
- Allow programs to behave based on user-provided data
- Present meaningful results
How to use Input and Output?
1. Using input() function
python
name = input("Enter your name: ") print("Hello, " + name)
# Prompts user for input and prints a greeting.
2. Using print() function
python
print("Welcome to Python!") print("Sum of 2 and 3 is", 2 + 3)
When to use Input and Output?
- Use
input()when you need data from users at runtime. - Use
print()to debug, display results, or send messages.
Advanced Usage
Formatted Output with f-strings
python
name = "Alice" age = 30 print(f"My name is {name} and I am {age} years old.")
Custom separator and end in print()
python
print("Hello", "World", sep="---", end="!!!\n")
Pros and Cons
Pros
- Simple and beginner-friendly
- Flexible: can handle strings, numbers, and more
- Interactive for console-based apps
Cons
input()always returns a string — must convert type manually- Less suitable for large-scale data input/output
- No built-in formatting in older Python versions
Conclusion
Python's input/output system is intuitive and powerful, especially for beginners. Mastering input() and print() is essential to writing interactive and informative programs.