Skip to content

Question 5

The following Python expression is entered into a fresh Python shell with no prior import statements. Determine the result from evaluating the following expression. [3 marks]

print(('dx'[1] + 'dy'[0]) * 2)
Solution

Here, it is important to remember that iterables and strings work on zero-indexing. Namely, a iterable (resembing a sequence, like a contiguous string of characters) typically begins with index 0.

With regards to the + and * operators pertaining to

('dx'[1] + 'dy'[0]) * 2
= ('x' + 'd') * 2
= 'xd' * 2
= 'xdxd'
Answer
'xdxd'