Python bootcamp · Lab 14

Reverse a word

easyStrings8 minLesson: Slicing Strings

Read the question, write Python on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read one word and print it reversed.

Python slicing has a slick idiom for this: a step of -1 walks the string backwards.

Input. One line: a word.

Output. The word, reversed.

Examples

Example 1
Input
code
Output
edoc
Example 2 — Case is preserved, just reordered.
Input
Py
Output
yP
Hint
  1. word[::-1] reverses any sequence.
  2. Use .strip() to drop a stray trailing newline before reversing.
Show correct code

Peek only after you have tried. You can still Check your own version.

word = input().strip()
print(word[::-1])
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.