Concepts / Printing Output to the Screen

Printing Output to the Screen

When output is unexpected, do not inspect only the final print statement.

  • Programming

From Prompt to Stored Text

A program can communicate with a person in two directions: it can display a prompt, and it can receive a response. The raw_input() function connects the keyboard to the program. It displays a prompt, waits for the user to type and press Enter, and returns the typed text as a string.

Keyboardtyped textraw_input()captures textsomethinga stringProgram logicuses the textuser types and presses Enterreturns textpasses stored value
How does text move from the keyboard through raw_input() and get stored in a variable as a string?
python

For the statement something = raw_input("Enter text: "), the events occur in order. First, the prompt string is displayed. Next, the program waits. The user types a response and presses Enter. raw_input() captures that response and returns it. Finally, the returned text is stored in the variable something.

The String Boundary

Definition

raw_input() captures text from the keyboard and returns it as a string to your program.

The value returned by raw_input() is text, even when the user types characters that look like a number. If your program needs numeric operations, convert the returned string to int or float before performing those operations.

Promptdisplay messageWaituser respondsCapturereturn stringProcessapply logicDisplayprint resultthenafter Enterstored textcomputed result
What happens next after the program prompts the user, captures the response, applies logic, and prints a result?

A Complete Palindrome Check

A palindrome is a word or phrase that reads the same forwards and backwards. A program can check one by reversing the captured string and comparing the reversed version with the original. The example below follows the source's three-part design: reverse() flips the string, is_palindrome() compares the two versions, and the main program captures input and prints the result.

Code walkthrough
def reverse(text):
    return text[::-1]

def is_palindrome(text):
    return text == reverse(text)

text = raw_input("Enter text: ")

if is_palindrome(text):
    print "The text is a palindrome."
else:
    print "The text is not a palindrome."
  1. 1
    The function reverse() receives text and returns that text in reverse order.
  2. 2
    The function is_palindrome() calls reverse() and compares the returned string with the original text.
  3. 3
    The raw_input() call displays its prompt, waits for input, and stores the returned string in text.
  4. 4
    The if statement sends the stored text to is_palindrome().
  5. 5
    The program prints one of two results according to the comparison.
  • reverse() uses slice notation [::-1] to flip the string backwards.
  • is_palindrome() compares the original text with its reversed version.
  • raw_input() captures the user's text and stores it in text.
  • The if statement selects which result to print.
Output
The displayed result depends on the text entered by the user.
Worked example

Tracing a Palindrome Response

Trace the stages of the palindrome checker when the user enters a palindrome.

  1. 1
    Input capture

    The prompt is displayed. The user types text and presses Enter. raw_input() returns that response as a string and stores it in text.

  2. 2
    Palindrome checking

    is_palindrome() receives the stored string, reverse() produces the backwards version, and the two strings are compared.

  3. 3
    Branch selection

    If the original and reversed strings match, the condition is true and the first print statement runs.

  4. 4
    Output

    The program displays the message that the text is a palindrome.

Result

The response travels from raw_input() to the variable, then through the checking functions, and finally to the selected output.

Captured textstring in textReversed textresult of reverse()Strings matchcomparison resultPalindrome resultfirst print branchNon-palindrome resultelse print branchreverse()compare originalcompare reversedtruefalse
How does the stored string determine which branch of the program executes and what output is produced?

Debugging by Stages

When output is unexpected, do not inspect only the final print statement. Trace the data step by step and ask what the program knows at each stage. For an input-based program, the important stages are input capture, data processing, and output. The unexpected result must come from one of these stages.

Expected resultwhat should happenInput capturewas text received?Processingwas logic applied?Outputwas result displayed?Divergence pointtrace this stagetrace firstinput is correctlogic is correctinput differslogic differsdisplay differs
At which step does the user's input or the program's interpretation diverge from the expected result?
Best practice

Trace the execution in order: confirm what the user entered, confirm the value passed into the processing logic, and then confirm which output branch ran. This separates an input problem from a processing problem and an output problem.

  • Common mistake

    Treating the result of raw_input() as a number automatically

    What it looks like

    A user types characters that look like a number, but raw_input() still returns text.

    Why it fails

    raw_input() returns a string, so numeric operations require conversion.

    Do this instead

    Convert the returned string to int or float when numeric operations are needed.

  • Common mistake

    Debugging only the final output line

    What it looks like

    The program prints an unexpected result, so attention goes straight to print.

    Why it fails

    The divergence may have occurred during input capture or processing.

    Do this instead

    Trace input capture, processing, and output step by step.

  • Common mistake

    Ignoring whitespace or case when comparing text

    What it looks like

    The entered text is processed as a string, but its spaces or letter case differ from what the logic expects.

    Why it fails

    String comparisons depend on the text being compared.

    Do this instead

    Handle whitespace and case sensitivity carefully and test with real input.

Practice the Data Flow

EASY

Write down the four stages for a program that uses raw_input() to receive text and then prints a result based on that text. For each stage, state what the program is doing and what value is available.

Hints
  • Begin with the prompt and the program waiting.
  • Identify the point at which the user's response becomes a string in a variable.
  • Separate the program's processing logic from its final output.
MEDIUM

Extend the palindrome-checking idea with a test plan. Choose text inputs that let you check a palindrome, a non-palindrome, and text whose whitespace or letter case needs careful consideration. For each input, predict which output branch should run, then trace the actual execution stages.

Hints
  • Check the captured string before examining the comparison.
  • Compare the original text with the result of reversing it.
  • If the result differs from your prediction, identify whether the divergence occurred during capture, processing, or output.

Key Takeaways

  1. 1
    raw_input() displays a prompt, waits for keyboard input, and returns the response as a string.
  2. 2
    The returned string can be stored in a variable and passed to program logic.
  3. 3
    Convert the string to int or float when numeric operations are required.
  4. 4
    Input-based programs can be understood and debugged by tracing capture, processing, and output in order.
  5. 5
    A complete program can use captured text, apply logic such as a palindrome comparison, and print the selected result.

At a glance

  • raw_input() is the gateway from the user's keyboard into the program.
  • The value returned by raw_input() is a string and may need conversion for numeric operations.
  • A prompt-and-response program moves through input capture, processing, and output.
  • Tracing those stages helps locate the point where actual behavior diverges from expectations.
  • The palindrome checker demonstrates how captured text can be passed through functions and used to select printed output.

Related concepts

More concepts starting with P