Combining Input and Output#

Note

Source: Adapted from the C# edition (data/io.rst) and the SE4ML Python chapter (chapter_python.rst, lines 781–786). The SE4ML section was itself marked as a todo; full content here is an original expansion covering input(), type conversion, and combined patterns.

Most useful programs need to communicate with the user — asking for data and reporting results. In Python this is done with input() and print().

The input() Function#

input() displays a prompt string, waits for the user to type something and press Enter, and then returns what the user typed as a string:

>>> name = input("What is your name? ")
What is your name? Alice
>>> name
'Alice'
>>> type(name)
<class 'str'>

The result is always a string, regardless of what the user types.

Converting Input to Numbers#

Since input() always returns a string, you must convert to a number if you need arithmetic. Use int() for integers or float() for decimal numbers:

>>> age_str = input("Enter your age: ")
Enter your age: 20
>>> age = int(age_str)
>>> age + 1
21

You can combine the two steps on one line, which is the most common pattern:

>>> age = int(input("Enter your age: "))
Enter your age: 20

>>> length = float(input("Enter room length: "))
Enter room length: 20.5
>>> type(length)
<class 'float'>

This is the Python equivalent of C#’s:

// C# — not Python
string s = Console.ReadLine();
double length = double.Parse(s);

But written more concisely on one line.

Warning

If the user types something that cannot be converted — for example, typing "abc" when you call int() — Python raises a ValueError and the program stops. We cover how to handle this gracefully in the While Loops chapter.

A Complete Input/Output Example#

Here is a small program that reads two numbers and prints their sum:

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print(f"The sum is {a + b}.")

Sample run:

Enter first number: 3.5
Enter second number: 1.5
The sum is 5.0.

Order Matters#

The prompt string in input() is shown before reading, so the user sees what to type. This is equivalent to the C# pattern of Console.Write followed by Console.ReadLine.

Compare:

# Python
name = input("Enter your name: ")

// C# equivalent — not Python
Console.Write("Enter your name: ");
string name = Console.ReadLine();