Skip to content

Question 5

Evaluate:

'Secret Gardens'[9::-1]
Hint

Evaluate each substring value in the expression.

Key:

  • 'string'[val] produces the character in index val from 'string'.
  • 'string'[start:stop:step]: start from the character at index start, stop before the character at index stop, and include characters in accordance to the step value (1 by default)
Solution

If it were just 'Secret Gardens'[9:] or 'Secret Gardens'[9::], it would evaluate to 'rdens' with the default step value being simply 1. A step value of -1, however, is the same as going through the characters of the string backwards (think how reverse() works with lists).

Hence, while it still starts with 'r', because we track backwards here, it produces 'raG terceS'.

Answer
'raG terceS'