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.
1# Take string input2s = input("Enter a string: ")34# Toggle case5result = ""6for char in s:7 if char.isupper():8 result += char.lower()9 elif char.islower():10 result += char.upper()11 else:12 result += char1314print(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
Lower uppercase characters.
- 2
Uppercase lowercase characters and copy other characters.
- 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.
๐Related Content
- ๐
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- ๐
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- ๐ป
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum