ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
def shift(s, k):
    return ''.join(chr((ord(c) - 97 + k) % 26 + 97) if c.islower() else c for c in s)
print(shift('xyz ABC', 3), shift('abc', -1))
  1. A. abd ABC zab
  2. B. abc ABC abc
  3. C. abc ABC zab
  4. D. xyz ABC zab

Correct answer: C. abc ABC zab

Python's % keeps the shifted index between 0 and 25 even when k is negative, so a moves to z. Uppercase letters are left alone because only lowercase letters are shifted.

More Python Programming questions

Exams that ask this topic