Python

In Python, handling Input and Output (I/O) is fundamental for interacting with users. Based on the lesson structure you are managing in your Python Basics module, here is a breakdown of how these functions work.

1. Output: The print() Function

The print() function is used to display data to the standard output device (usually your screen).

  • Basic Usage: You can pass strings, numbers, or variables directly.

  • Multiple Arguments: Use commas to print multiple items; Python automatically adds a space between them.

  • Formatting: You can use f-strings (formatted string literals) for a clean way to include variables inside strings.

Python
name = "Admin"
print("Hello, World!") 
print("User:", name)
print(f"Welcome back, {name}!") # f-string method

2. Input: The input() Function

The input() function allows you to pause program execution and wait for the user to type something.

  • Default Type: Crucially, input() always returns a string.

  • Prompt: You can pass a string to the function to display a message to the user.

Python
user_name = input("Enter your name: ")
print(f"Hello, {user_name}")

3. Handling Numeric Input

Since input() gives you a string, you must use Type Casting (the next lesson in your module) to perform math. If you don't convert the input, Python will throw an error or concatenate the strings instead of adding them.

ScenarioCode Example
Integer Inputage = int(input("Enter age: "))
Float Inputprice = float(input("Enter price: "))

4. Useful Parameters for print()

You can customize how the print() function behaves using these optional arguments:

  • sep: Changes the separator between multiple objects (default is a space).

  • end: Changes what is printed at the very end (default is a newline \n).

# Custom separator
print("Python", "Basics", "Module", sep="-") 
# Output: Python-Basics-Module

# Prevent moving to a new line
print("Hello", end=" ")
print("World")
# Output: Hello World
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now