Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Python Input and Formatted Command-Line Output

Hello again. In the previous lesson, you stored fixed values such as an agent name, a retry limit, and an approval setting in Python variables. That is useful for configuration, but a program becomes interactive when it can receive information while it runs.

In this lesson, you will use input() to collect information from someone using your script and print() to respond with clear, formatted command-line output. You will also see why input that looks like a number must be converted before Python can treat it as one. These are the beginnings of a command-line interface: a small, text-based conversation between a person and a program.


Input and output: a short conversation

A command-line program usually follows a simple rhythm:

  1. It displays a prompt or instruction.
  2. It pauses for the user to type something.
  3. The user presses Return.
  4. The program stores that response in a variable.
  5. The program displays a result, summary, or next instruction.

Python has two built-in functions for this:

  • input() collects keyboard input.
  • print() displays output in the terminal.

For example:

name = input("What is your name? ")
print("Hello", name)

When Python reaches the first line, it displays the prompt and pauses. It does not move to the print() line until the user types a response and presses Return.

A terminal session could look like this:

What is your name? Maya
Hello Maya

Here, Maya is typed by the person running the program. Python stores it in the variable name, then uses that stored value in the output.

The text inside input(...) is called the prompt. Prompts should explain exactly what the program expects. Compare these:

name = input("> ")
name = input("Enter your name: ")

Both work, but the second is much clearer. Clear prompts become particularly important in automation tools, where someone may need to confirm an action, enter a file name, or choose a safe operating mode.

Basic Input and Output in Python

Read “Basic Input and Output in Python” from Real Python for a concise explanation of how terminal input pauses a program, how prompts work, and how print() displays values.

In the section “Reading Input From the Keyboard,” begin at the explanation that introduces input. Focus on the fact that input() waits for Return and gives Python the characters the user typed. Continue within that same section from the paragraph beginning “Even if you type characters that resemble numbers” through the discussion that says Python may convert numeric input. Then read “Writing Output to the Console” through the explanation that print() separates its arguments with a space and adds a default newline.


input() always returns text

The most important rule in this lesson is:

input() always returns a string (str).

Suppose a person types 5 at this prompt:

max_tool_calls = input("Maximum tool calls: ")

It may look like Python received the number 5, but it actually received the text "5".

You can verify this temporarily with type():

max_tool_calls = input("Maximum tool calls: ")
print(type(max_tool_calls))

If you enter 5, Python displays:

<class 'str'>

This matters when you want to calculate with the value. This code does not work:

max_tool_calls = input("Maximum tool calls: ")
next_limit = max_tool_calls + 1

Python cannot add the integer 1 to text such as "5".

When the user is expected to enter a whole number, convert their response immediately with int():

max_tool_calls = int(input("Maximum tool calls: "))
next_limit = max_tool_calls + 1

print(next_limit)

Read the first line from the inside out:

  1. input("Maximum tool calls: ") collects text from the user.
  2. int(...) converts text such as "5" into the integer 5.
  3. The converted integer is assigned to max_tool_calls.

For a decimal number, use float() instead:

confidence_threshold = float(input("Confidence threshold: "))

For now, use int() only when you clearly expect a whole number, such as a retry limit or a maximum number of tool calls. Use float() when decimal values make sense, such as a threshold of 0.8.

If a person enters three where your code expects an integer, Python will raise a ValueError. That is not a failure of the idea of conversion; it is Python reporting that the entered text cannot be converted to the requested type. In the next lesson, you will learn to read such error messages systematically.


Produce readable output with print() and f-strings

You already used print() to display a variable. It can also display several values at once:

builder_name = "Maya"
print("Builder:", builder_name)

Output:

Builder: Maya

When print() receives comma-separated values, it puts a space between them by default. This is useful for quick debugging and simple messages.

For deliberate, readable output, use an f-string. An f-string lets you put variable values directly into text:

builder_name = "Maya"
print(f"Builder: {builder_name}")

The f before the opening quote tells Python to format the string. Curly braces mark the place where Python should evaluate an expression and insert its value.

max_tool_calls = 5
print(f"Maximum tool calls: {max_tool_calls}")

Output:

Maximum tool calls: 5

Notice two details:

  • The variable inside {} has no quotation marks. You want its value.
  • An f-string can display an integer, float, Boolean, or string without you manually calling str() first.

This is different from joining strings with +:

max_tool_calls = 5

# This causes a TypeError:
# print("Maximum tool calls: " + max_tool_calls)

The + approach requires both sides to be strings, so you would need str(max_tool_calls). An f-string is usually clearer:

print(f"Maximum tool calls: {max_tool_calls}")

Python For Beginners - Inputs & Outputs Explained

Watch “Python For Beginners - Inputs & Outputs Explained” by TeachingCS to see input stored in variables, followed by two ways of incorporating that information into terminal output. The final segment introduces f-strings visually.

Watch input storage to see a prompt’s response assigned to a variable. Then watch comma output for the basic multi-argument form of print(). Finish with f string formatting. Pay attention to the two required pieces of f-string syntax: the f before the quote and the curly braces around each variable whose value should appear in the text.

A common f-string mistake is forgetting the f:

builder_name = "Maya"
print("Builder: {builder_name}")

Python treats that as ordinary text and prints:

Builder: {builder_name}

Add the f to make substitution happen:

print(f"Builder: {builder_name}")

Build an interactive agent-profile script

You will now create a small script that asks a person to describe a draft automation, converts one numeric response, and prints a structured summary.

In the terminal, return to the folder from the previous lessons:

cd ~/agent_builder/first_script

In VS Code, create a file named:

interactive_profile.py

Enter and save the following code:

# Collect a simple profile for a draft automation.

builder_name = input("Your name: ")
automation_name = input("Automation name: ")
max_tool_calls = int(input("Maximum tool calls: "))

print()
print("=== DRAFT AGENT PROFILE ===")
print(f"Builder: {builder_name}")
print(f"Automation: {automation_name}")
print(f"Maximum tool calls: {max_tool_calls}")
print("Mode: dry-run")

Run it in VS Code’s integrated terminal:

python3 interactive_profile.py

A sample run might look like this:

Your name: Maya
Automation name: Local research helper
Maximum tool calls: 4

=== DRAFT AGENT PROFILE ===
Builder: Maya
Automation: Local research helper
Maximum tool calls: 4
Mode: dry-run

The first three lines are interactive prompts. After each prompt, the script pauses until the user presses Return. The rest is formatted program output.

Let’s inspect the script piece by piece.

builder_name = input("Your name: ")

This stores the typed response as text. That is appropriate because a name is text.

automation_name = input("Automation name: ")

This also stores text. Even if someone names their automation "Agent 7", the value is still a string because it is a name, not a quantity to calculate with.

max_tool_calls = int(input("Maximum tool calls: "))

This stores an integer. In a later agent loop, a limit like this can help prevent an agent from making unlimited tool calls. It must be a number because a program will need to compare, count, or adjust it.

print()

Calling print() with nothing inside the parentheses displays one blank line. Here, it visually separates user input from the final report.

print("=== DRAFT AGENT PROFILE ===")

This prints a heading. The equal signs are ordinary characters, used only to make the command-line output easier to scan.

print(f"Builder: {builder_name}")

This combines a fixed label, "Builder: ", with the value stored in builder_name.

The last line:

print("Mode: dry-run")

uses fixed text because this script has not yet asked the user to choose a mode. Later, once you learn conditional statements, your code will be able to make decisions based on user choices. For now, the important distinction is between a literal piece of text and a variable value inserted with an f-string.


Make small, meaningful changes

Use the same script to practice the edit-save-run cycle. Make one change at a time, save the file, and rerun it.

First, add an optional text description:

description = input("One-sentence description: ")

Place it after the automation name prompt. Then add this line to the report:

print(f"Description: {description}")

Next, add a decimal setting:

confidence_threshold = float(input("Confidence threshold: "))

Then print it in the report:

print(f"Confidence threshold: {confidence_threshold}")

Enter a decimal such as 0.8, not text such as high. You have told Python to convert this response with float(), so Python expects a decimal-compatible number.

Finally, try intentionally entering invalid numeric data, such as many for maximum tool calls or 0.8 for a value wrapped in int(). Your script will stop and show an error. Do not worry about fixing it with more code yet. Read the last line of the message and notice that it identifies the failed conversion. That observation will give you useful material for the next lesson on interpreting tracebacks.


Key takeaways

You can now make a Python script interact with a person in the terminal:

  • input("Prompt: ") displays a prompt, pauses the script, and returns what the user typed.
  • Every result from input() starts as a str, even input that looks like a number.
  • Use int() for whole-number input and float() for decimal input when you need numerical values.
  • Use print() to display information in the terminal.
  • Comma-separated arguments to print() are quick and Python places spaces between them.
  • F-strings, such as f"Builder: {builder_name}", are a clear default for formatted output.
  • print() on its own creates a blank line, which can make terminal output easier to read.

Keep interactive_profile.py. It is your first interactive command-line program, and the numeric conversion errors you may have seen are a useful lead-in to the next lesson: reading Python tracebacks to correct basic syntax, name, and type errors.

Can't find a good explanation? Sign up and we'll make it for you

Sign up