</> Code Editor { } Code Formatter

Python Basic Input and Output Exercises


Python Basic Input and Output Practice Questions

1/20
Correct
0%

Which of the following functions is used in Python to display output on the screen?


In Python, the print() function is used to send output to the console. It can handle text, numbers, variables, and even multiple values at once.

Example:

print("Hello, World!")
print("Sum =", 5 + 3)

Why others are wrong:

  • Option 1 (display()) → Exists in Jupyter environments for rich display, not standard Python.
  • Option 2 (echo()) → Used in other languages/shells, not Python.
  • Option 3 (show()) → Sometimes used in libraries (like matplotlib), but not for general output.

Thus, print() is the standard and correct way for output in Python.

Quick Recap of Python Basic Input and Output Concepts

If you are not clear on the concepts of Basic Input and Output, you can quickly review them here before practicing the exercises. This recap highlights the essential points and logic to help you solve problems confidently.

What Is Output in Python?

You use the built-in print() function to display output on the screen.

print() can take multiple items (strings, variables, expressions) and separate them with commas. By default, it separates them with a space.

You can control how things are printed using keyword parameters:

  • sep: defines what string is put between the items.
  • end: defines what is added at the end (instead of the default newline \n).

Example:

first = "Python"
second = "Rocks"
print(first, second, sep=" -- ", end="!\n")
# Output: Python -- Rocks!

What Is Input in Python?

The input() function pauses your program and waits for the user to type something, then returns that as a string.

You can optionally provide a prompt inside input(...), which will be shown to the user before they type.

Example:

city = input("Where do you live? ")
print("You live in", city)

Converting User Input (Casting) in Python

Since input() always returns a string, you often need to convert (“cast”) that string into another type if you want to do calculations.

Use int() to convert to integer, float() to convert to decimal numbers.

Example:

age_str = input("Enter your age: ")
age = int(age_str)
print("In five years, you will be", age + 5)

Be careful: if the user types something that can’t be converted (like “abc”), it will cause an error at runtime. Good programs should check or validate input before casting.

Taking Multiple Inputs in Python

You can read more than one value from a single input() call by using .split() to divide the input string into parts.

Example:

x, y = input("Enter two numbers separated by space: ").split()
x = int(x)
y = int(y)
print("First:", x, "Second:", y)

Advanced print() Parameters & Behavior in Python

print() supports more than just sep and end: you can also redirect output with file or flush the output buffer with flush.

Example of using end in a loop, to print items on the same line:

for i in range(5):
    print(i, end=" ")
print()  # This moves to the next line afterward
# Output: 0 1 2 3 4

Best Practices & Tips for Input & Output in Python

  • Always provide a clear prompt in input() so users understand what they need to type.

  • Validate or sanitize user input before converting it to a number to avoid runtime errors.
  • Use formatting (like f-strings) to make your print output cleaner and more readable.
  • Be careful with long or complex inputs — splitting, converting, and handling them properly makes your program more robust.


About This Exercise: Python – Basic Input and Output

Welcome to Solviyo’s Python Basic Input and Output exercises. These exercises are designed to help beginners understand how Python programs interact with users, allowing you to both collect information and display meaningful results. Input and output are fundamental skills that form the backbone of many programs, and practicing them early will build your confidence and coding proficiency.

What You Will Learn

In this set of exercises, you will explore essential input and output concepts, including:

  • Using the input() function to read data from users.
  • Displaying results with the print() function in a readable and professional way.
  • Formatting output using f-strings and other string formatting techniques.
  • Combining multiple variables into a single output.
  • Performing calculations with numeric input and handling type conversion between strings, integers, and floats.
  • Designing interactive, real-world scenarios that make learning practical and relevant.
  • Best practices for clean, readable, and maintainable input/output code.

Why Learning Python Input and Output Matters

Mastering input and output is a key step in becoming an effective Python programmer. These skills allow your programs to interact with users, take dynamic data, and display results in a clear and structured way. A solid understanding of I/O also prepares you for advanced topics like file handling, user-driven applications, and error handling.

Start Your Python Journey with Input and Output

By practicing these exercises, you will gain the confidence to handle user input, display formatted results, and build simple interactive programs. This foundation is essential for progressing to more complex Python topics and real-world projects. Start working on Python Basic Input and Output exercises today, and take the first step toward creating functional, user-friendly programs.