Part of the series

Several example codes

~1 min read • Updated Oct 12, 2025

Program Overview

This Python program reads a character from the user and toggles its case using the XOR (^) operator.
In ASCII, the difference between uppercase and lowercase letters is 32, so XOR with 32 flips the case.


Python Code:


def toggle_case(char: str) -> str:
    if 'A' <= char <= 'Z' or 'a' <= char <= 'z':
        return chr(ord(char) ^ 32)
    return char

# Read character from user
char = input("Enter a character: ")

# Convert and display
converted = toggle_case(char)
print(f"Toggled character: {converted}")

Sample Output:


Input: A  
Output: a

Input: g  
Output: G

Written & researched by Dr. Shahin Siami