Python ASCII & Unicode Lab

Lab: The “Hidden Numbers” Workshop

Objective: Understand how computers store text and manipulate characters using ord() (to number) and chr() (to text).


Experiment 1: The “Secret Decoder” Ring (Caesar Cipher)

Goal: Create a simple encryption script by shifting ASCII values.

Scenario: You want to send a secret message by shifting every letter forward by 1 (A becomes B, B becomes C).

Step 1: Run the Encryption Code

# The message to hide
message = "HELLO"
encrypted_text = ""

print(f"Original: {message}")

for char in message:
    # 1. Convert char to ASCII number
    ascii_val = ord(char)
    
    # 2. Shift the number by +1
    shifted_val = ascii_val + 1
    
    # 3. Convert back to character
    new_char = chr(shifted_val)
    
    # Append to result
    encrypted_text += new_char

print(f"Encrypted: {encrypted_text}")

Observation: HELLO becomes IFMMP. You just performed basic cryptography!


Experiment 2: The “Magic 32” (Manual Case Conversion)

Goal: Understand why computers see ‘a’ and ‘A’ as different, and how to convert them mathematically.

Context: The difference between any Uppercase letter and its Lowercase version in ASCII is exactly 32.

  • 'a' (97) – 'A' (65) = 32

Step 1: Run the Manual “Lowercaser”

def manual_to_lower(text):
    result = ""
    for char in text:
        # Check if character is Uppercase (A-Z are 65-90)
        if 65 <= ord(char) <= 90:
            # Add 32 to "slide" it into the lowercase range
            new_char = chr(ord(char) + 32)
            result += new_char
        else:
            # Keep punctuation or existing lowercase as is
            result += char
    return result

original = "PYTHON Code!"
lowered = manual_to_lower(original)

print(f"Original: {original}")
print(f"Manual Lower: {lowered}")

Observation: You just recreated Python’s built-in .lower() method using raw math!


Experiment 3: The “Password Strength” Validator

Goal: Use ASCII ranges to validate input without using helper methods like .isdigit().

Scenario: A password must contain at least one Capital Letter and one Number.

def check_password(password):
    has_upper = False
    has_digit = False
    
    for char in password:
        code = ord(char)
        
        # Check ASCII range for 'A' to 'Z' (65-90)
        if 65 <= code <= 90:
            has_upper = True
            
        # Check ASCII range for '0' to '9' (48-57)
        elif 48 <= code <= 57:
            has_digit = True
            
    if has_upper and has_digit:
        return "✅ Password Strong"
    else:
        return "❌ Password Weak (Needs Uppercase & Number)"

# Test the function
print(check_password("pass123"))   # Weak (No Upper)
print(check_password("Passcode"))  # Weak (No Number)
print(check_password("Pass123"))   # Strong

Experiment 4: Unicode & Emojis

Goal: Prove that Python can handle numbers way bigger than 127 (ASCII).

Step 1: Run the Emoji Generator We will print characters using their specific Hexadecimal Unicode points.

# Unicode uses hex numbers (e.g., 0x1F600)
# You can find these codes online at unicode.org

print("Here are some Unicode characters:")
print(f"U+03A9 (Greek Omega): {chr(0x03A9)}")
print(f"U+2602 (Umbrella):    {chr(0x2602)}")
print(f"U+1F680 (Rocket):     {chr(0x1F680)}")
print(f"U+1F40D (Snake):      {chr(0x1F40D)}")

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top