livepython.net

191. Python Program to Reverse a String

EasyPhase 1 · Warm-up

This one needs only a for loop and a string that grows in it. You know both.

Overview

Reversing a string is one of the most common warm-up questions in coding interviews. It shows that you can walk through text one character at a time.

Problem Statement

Aiko writes each secret note backwards, so that it is harder to read.

Read one line of text and print it backwards, as Reversed: <text>. Every character moves, also the spaces.

Steps

  1. Read the line of text.
  2. Start with an empty string for the result.
  3. Loop over the characters. Put each one in front of the result.
  4. Print Reversed: and then the result.

Examples

Example 1. Input hello gives:

Reversed: olleh

Why: the last letter o comes first, and the first letter h comes last.

Example 2. Input Good morning gives:

Reversed: gninrom dooG

Why: the whole line turns around. The space moves too, and the capital G ends up last.

Input

One line of text: a word or a sentence (1 character or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line: Reversed: and then the text backwards. The tests try 5 different inputs.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 10 · For Loops
solution.py
# write your code here

EXPECTED OUTPUT

1Reversed: olleh

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.