String Operations and Slicing
raw_input() captures text from the keyboard and returns it as a string to your program.
From Keyboard to Variable
A program can receive text from outside itself through raw_input(). This function displays a prompt, waits for the user to type text and press Enter, and returns the entered text as a string. When that returned value is assigned to a variable, the rest of the program can process it.
- The prompt string is displayed.
- The program waits for the user.
- The user types text and presses Enter.
- raw_input() captures and returns that text.
- The returned string is stored in something.
Strings Through Program Logic
After raw_input() returns, the response is available as a string value. The program can pass that string to other operations and functions. In the palindrome example, the entered text is passed to a checking function. That function compares the original text with a reversed version. If both versions match, the text is a palindrome.
A palindrome is a word or phrase that reads the same forwards and backwards.
def reverse(text): return text[::-1] def is_palindrome(text): return text == reverse(text) text = raw_input("Enter text: ") result = is_palindrome(text) print result
Reversing with Slice Notation
The palindrome check depends on the slice notation [::-1]. In this program, that notation flips the string backwards. The important comparison is between two values: the original text and the reversed text. Matching values indicate a palindrome; different values indicate that the entered text does not read the same forwards and backwards.
Tracing a palindrome response
A user enters text, and the program must determine whether the text is a palindrome.
Input capture: raw_input() displays the prompt, waits for the response, and returns the entered text as a string.
Reversal: reverse() applies [::-1] to produce the text in backward order.
Comparison: is_palindrome() compares the original response with the reversed response.
Decision result: If the two strings match, the response is a palindrome.
The program's result depends on the comparison between the captured string and its reversed version.
Finding the Divergence
When output is unexpected, do not inspect only the final result. Trace the program step by step and identify the state of the data at each major stage. For the palindrome checker, inspect input capture, palindrome checking, and output. This narrows the problem to the point where the actual result first differs from the expected result.
Treating the value returned by raw_input() as a number automatically.
raw_input() returns a string.
Fix:
Convert the returned string to int or float when numeric operations are required.Debugging only the printed result.
The divergence may have occurred during input capture, processing, or output.
Fix:
Trace the execution step by step and inspect the data at each stage.Forgetting that the prompt, the typed response, and the returned string are different parts of the input process.
The prompt is displayed first; raw_input() then waits for the user to type text and returns that typed text.
Fix:
Separate the prompt display, user entry, returned string, and variable assignment when tracing the program.
Practice the Trace
Write down the value at each stage of a palindrome-checking program: the text entered by the user, the value returned by reverse(), the comparison performed by is_palindrome(), and the final printed result. Then identify which stage you would inspect first if the output did not match your expectation.
Hints
- Begin with the fact that raw_input() returns a string.
- Use [::-1] to represent the reversal stage.
- Compare the original string with the reversed string before considering the printed output.
- Identify the prompt shown to the user.
- Record the text typed and returned by raw_input().
- Record the reversed value produced by [::-1].
- Compare the original and reversed values.
- Use the comparison result to explain the program's output.
Key Takeaways
- raw_input() displays a prompt, waits for keyboard input, and returns the entered text as a string.
- The returned string is stored in a variable and can then be passed through program logic.
- The slice notation [::-1] reverses a string, making it useful for comparing text with its backward form.
- A palindrome checker compares the original string with its reversed version.
- To debug unexpected output, trace input capture, processing, and output step by step.
Key Takeaways
- raw_input() captures keyboard text and returns it as a string.
- The captured response can be stored, reversed with [::-1], and compared with the original.
- A palindrome is detected when the original text and its reversed version match.
- Tracing each program stage helps locate whether an unexpected result began during input, processing, or output.