Swap the Case of a Sentence

Easy ⏱ 8 min 86% acceptance ★★★★☆ 4.4
Write swap_case(s) that flips every uppercase letter in s to lowercase and every lowercase letter to uppercase, leaving digits, punctuation and spaces untouched.

Examples

Example 1
Input
s = 'Hello World'
Output
'hELLO wORLD'
Explanation

Each cased letter flips; the space is unaffected.

Example 2
Input
s = 'Py3.12 Rocks!'
Output
'pY3.12 rOCKS!'
Explanation

Digits and punctuation pass through unchanged.

Constraints

  • 0 <= len(s) <= 10^4

Topics

StringsCase Conversion

Companies

MicrosoftAccenture

Hints

Hint 1

Python strings have a built-in method for exactly this.

Hint 2

Check `str.swapcase()`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.