Fundamentals 5 min read

Using Zero‑Width Characters in Python for Steganography and Text Control

The article explains what zero‑width Unicode characters are, lists common types, describes their applications such as steganography and text formatting, and provides Python code examples for inserting, hiding, extracting, bypassing filters, and removing these invisible characters.

Subtle Storm
Subtle Storm
Subtle Storm
Using Zero‑Width Characters in Python for Steganography and Text Control

What Are Zero‑Width Characters

Zero‑Width Characters are Unicode symbols that occupy a position in a string but render no visible width. They are used for steganography, format control, and other text‑processing tasks.

Zero Width Non‑Joiner (U+200C)

Zero Width Joiner (U+200D)

Zero Width Space (U+200B)

Word Joiner (U+2060)

Uses of Zero‑Width Characters

Steganography: Insert invisible characters to hide information.

Avoiding automated detection: Insert characters to bypass crawlers or filters.

Text formatting control: Influence character joining in scripts such as Arabic or Hindi.

Data watermarking: Embed invisible characters as a provenance marker.

Inserting Zero‑Width Characters (Python)

# Insert a zero‑width space (U+200B)
text = "hello"
hidden_text = text[0] + "\u200B" + text[1:]
print("Original text:", text)          # Original text: hello
print("Text with zero‑width space:", hidden_text)  # Appears as hello but contains a hidden character
print("Contains zero‑width space:", "\u200B" in hidden_text)  # True

Hiding and Extracting Information (Python)

def hide_message(base_text, hidden_message):
    """Encode hidden_message into base_text using zero‑width spaces"""
    zero_width_space = "\u200B"
    binary_message = ''.join(format(ord(c), '08b') for c in hidden_message)
    encoded_message = ''.join(zero_width_space if bit == '1' else '' for bit in binary_message)
    return base_text + encoded_message

def extract_message(base_text, encoded_text):
    """Extract hidden_message from encoded_text"""
    zero_width_space = "\u200B"
    hidden_binary = ''.join('1' if ch == zero_width_space else '0' for ch in encoded_text)
    chars = [chr(int(hidden_binary[i:i+8], 2)) for i in range(0, len(hidden_binary), 8) if hidden_binary[i:i+8]]
    return base_text + ''.join(chars)

base_text = "This is a test."
hidden_message = "Hi"
encoded_text = hide_message(base_text, hidden_message)
print("Encoded text:", encoded_text)
extracted_message = extract_message(base_text, encoded_text)
print("Extracted text:", extracted_message)

Bypassing String Detection (Python)

# Simulate a sensitive‑word filter
def contains_sensitive_word(text, sensitive_word):
    return sensitive_word in text

sensitive_word = "secret"
modified_text = sensitive_word[0] + "\u200B" + sensitive_word[1:]
print("Contains sensitive word:", contains_sensitive_word(modified_text, sensitive_word))  # False

Removing Zero‑Width Characters (Python)

def remove_zero_width_chars(text):
    """Remove all zero‑width characters from text"""
    zero_width_chars = ['\u200B', '\u200C', '\u200D', '\u2060']
    for ch in zero_width_chars:
        text = text.replace(ch, '')
    return text

text_with_zero_width = "hello\u200Bworld\u200C!"
cleaned_text = remove_zero_width_chars(text_with_zero_width)
print("Original:", text_with_zero_width)
print("Cleaned:", cleaned_text)

Zero‑width characters are powerful for text processing, steganography, and data marking, but they should be used cautiously in security‑sensitive contexts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonsecurityUnicodetext processingsteganographyzero-width charactersdata watermarking
Subtle Storm
Written by

Subtle Storm

The micro era's marvels are boundlessly subtle.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.