Input and Output in Python

Subject: Data Structures and Algorithms · Language: Python · Level: beginner · 6 min read

Learn print, input, reading numbers, reading words, reading lists, f-strings, and Python DSA input-output habits.

Input means taking data into the program, and output means showing the result. In coding platforms, the input usually comes from hidden or visible test cases. Your program must read that input, solve the problem, and print the answer exactly in the required format.

The most important rule is that Python's `input()` function always returns a string. Even if the user enters `50`, Python receives it as text, not as a number. To use it in calculation, convert it using `int()` or `float()`. Many beginner mistakes happen because students forget this conversion.

When input has multiple values on the same line, `split()` is used to separate them. For example, if the line is `10 20 30`, `split()` gives three text values. Then `map(int, input().split())` converts each value into an integer. This pattern is very common in DSA.

Output must match the expected output exactly. Some problems want each answer on a new line. Some want values separated by spaces. Some want no extra message like `Enter number:`. In coding contests, extra words can make the answer wrong even if the logic is correct.

For lists, `print(*arr)` is a useful Python style because it prints all elements separated by spaces. But beginners should also practice printing with loops, because loops build the foundation for traversal and formatting.

A clean program usually has three mental parts: read input, process logic, and print output. Keeping these parts separate helps students debug faster. If the answer is wrong, first check whether input was read correctly, then check logic, then check output format.

Input means taking data from the user or from a test case. Output means showing the result produced by the program.

In coding practice, the online compiler usually provides input values, runs your program, and shows the output. This is why input and output are important before solving DSA problems.

Output Using print

Python uses the `print()` function to display output.

```python

print("Hello Python")

print(10)

```

Output

```text

Hello Python

10

```

By default, `print()` moves to the next line after printing.

Printing Multiple Values

You can print multiple values by separating them with commas.

```python

name = "Rahul"

marks = 85

print("Name =", name)

print("Marks =", marks)

```

Python automatically adds a space between comma-separated values.

Controlling End of Line

The `end` parameter controls what is printed after the output.

```python

print(1, end=" ")

print(2, end=" ")

```

Output

```text

1 2

```

This is useful when printing array or list values on the same line.

Input Using input

Python uses `input()` to read a line of text from the user.

```python

name = input()

```

Reading an Integer

To read an integer, convert the input using `int()`.

```python

age = int(input())

```

This is one of the most common patterns in beginner Python programs.

Reading a Decimal Value

To read a decimal value, use `float()`.

```python

percentage = float(input())

```

Use this for values such as average, percentage, price, or measurement.

Reading With Prompt Text

You can pass prompt text inside `input()`.

```python

name = input("Enter name: ")

```

The prompt is displayed before reading the value. In online judges, prompts are usually avoided, but in beginner lessons they make programs easier to understand.

Formatted Output Using f-strings

An f-string is a clean way to place variable values inside text.

```python

name = "Rahul"

marks = 85

print(f"Name = {name}")

print(f"Marks = {marks}")

```

Output

```text

Name = Rahul

Marks = 85

```

F-strings are readable and very useful in normal Python programs.

Reading Multiple Numbers

Many DSA problems give multiple numbers in one line. Use `split()` to break the line into parts and `map()` to convert them.

```python

a, b = map(int, input().split())

```

If input is `10 20`, then `a` becomes `10` and `b` becomes `20`.

Reading a List of Numbers

To read a list of integers from one line, use `list(map(int, input().split()))`.

```python

arr = list(map(int, input().split()))

```

This pattern is used constantly in array and list problems.

Example Program

```python

name = input("Enter name: ")

marks = int(input("Enter marks: "))

print(f"Name = {name}")

print(f"Marks = {marks}")

```

Sample Input

```text

Rahul

85

```

Output

```text

Enter name: Enter marks: Name = Rahul

Marks = 85

```

This program reads a student's name and marks, then prints both values in a clear format.

Input and Output in DSA

In DSA, input is often a number, a list size, list elements, a string, or multiple test cases. Output is usually the answer, such as sum, maximum, count, sorted list, or yes/no result.

For beginner Python, first become comfortable with `input()`, `int()`, `split()`, `map()`, and `print()`. These tools are enough for many starting problems.

Input and output are the connection between the program and the outside world. In coding practice, the input usually comes from test cases. The program reads those values, processes them, and prints the answer in the required format.

When multiple numbers are given on one line, `split()` is commonly used. It breaks the line into separate strings. Then `map(int, input().split())` converts each string into an integer. This pattern is used constantly in DSA problems.

Output must match the expected format. Sometimes one extra space or missing newline can cause a wrong answer on coding platforms. That is why students should read the output requirement carefully and print exactly what is asked.

For arrays or lists, a common output pattern is to print values separated by spaces. Python can do this using a loop, or by using `print(*arr)`. The star operator expands the list values as separate arguments to `print`.

A good practice is to separate input, logic, and output mentally. First read the values. Then solve the problem. Finally print the result. This makes even larger programs easier to debug.

Practice Question

Take a student's name, roll number, and marks as input. Print the details in separate lines and print whether the student passed if marks are greater than or equal to 40.

More Python Lessons

Browse all PrepCampus study materials