Input and output in Python allow programs to interact with users. Input lets users provide data, while output displays results or messages on the screen.
Python provides simple built-in functions to handle input and output efficiently.
Why Input and Output Are Important
Input and output help to:
- Make programs interactive
- Accept user data dynamically
- Display results clearly
- Build real-world applications
Example:
print("Welcome to Python")
Output in Python
Using print() Function
The print() function is used to display output.
print("Hello World")
print(10)
print(5 + 3)
Printing Multiple Values
name = "Aman"
age = 22
print(name, age)
Input in Python
Using input() Function
The input() function takes input from the user.
name = input("Enter your name: ")
print(name)
⚠️ Input is always treated as a string.
Type Conversion with Input
To use numeric input, type casting is required.
age = int(input("Enter age: "))
print(age + 1)
Formatted Output
Using f-strings
name = "Sagar"
course = "Python"
print(f"My name is {name} and I am learning {course}")
Using format() Method
print("My name is {} and age is {}".format("Aman", 25))
Escape Characters in Output
| Character | Meaning |
|---|---|
| \n | New line |
| \t | Tab |
| \ | Backslash |
Example:
print("Hello\nPython")
Common Input/Output Mistakes
❌ Forgetting type conversion
❌ Incorrect formatting
❌ Missing user prompts
Best Practices
✔ Always validate user input
✔ Use clear prompts
✔ Format output neatly
Conclusion
Input and output are essential for making Python programs interactive. Understanding them allows you to accept user data and present results clearly and professionally.
References
- Internal Reference: https://savanka.com/category/learn/python/
- External Reference: https://www.w3schools.com/python/