Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Toggle Case

Toggle letter case with a character loop. Students choose upper or lower conversions per character and copy digits and punctuation into a new result unchanged.

IntermediatePhase 5 - Category 2: Character OperationsExample 8 of 10
toggle-case.py
1# Take string input
2s = input("Enter a string: ")
3
4# Toggle case
5result = ""
6for char in s:
7 if char.isupper():
8 result += char.lower()
9 elif char.islower():
10 result += char.upper()
11 else:
12 result += char
13
14print(f"Toggled: {result}")

Output

Enter a string: Hello
Toggled: hELLO

In this one

  • Choose per-character conversion
  • Preserve uncased text
  • Rebuild an immutable string

How it works

  1. 1

    Lower uppercase characters.

  2. 2

    Uppercase lowercase characters and copy other characters.

  3. 3

    Append each chosen piece and print result.

Edge cases

What happens to digit 1?

What happens to digit 1?

It is copied unchanged.

Does empty input work?

Does empty input work?

Yes. The result remains empty.

What's going on

Uppercase characters are converted with lower, and lowercase characters with upper. The final else copies characters without either case.

The result is built in input order, so Ab1! becomes aB1!. Strings are immutable; repeated concatenation constructs new text rather than editing s. Unicode case conversions can expand a character, so output length is not guaranteed to stay equal.

What happens to digit 1? It is copied unchanged.

Does empty input work? Yes. The result remains empty.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What happens to uppercase A?

Pick an answer to check it.

Question 2

What happens to digit 1?

Pick an answer to check it.

Questions people ask

What happens to digit 1?

It is copied unchanged.

Does empty input work?

Yes. The result remains empty.

What happens to uppercase A?

It becomes a.