Pure Python implementation of FIGlet for creating ASCII art text from regular text using various fonts
—
Enhanced string processing capabilities for ASCII art including reversing, flipping, and whitespace normalization. FigletString extends standard string functionality with ASCII art-specific operations.
Enhanced string class that inherits from the standard string type while adding ASCII art manipulation methods.
class FigletString:
"""
ASCII art string with manipulation methods.
Inherits from unicode string type, providing all standard string methods
plus additional ASCII art manipulation capabilities.
"""Creates a horizontal mirror image of the ASCII art by reversing each line and translating characters to their mirrored equivalents.
def reverse(self) -> FigletString:
"""
Create horizontal mirror image of ASCII art.
Returns:
FigletString: Horizontally reversed ASCII art
Behavior:
- Reverses character order in each line
- Translates characters using reverse mapping (/->\\, etc.)
- Preserves line count and overall structure
"""import pyfiglet
# Create ASCII art
text = pyfiglet.figlet_format("Hello", font="standard")
print("Original:")
print(text)
# Reverse the text
reversed_text = text.reverse()
print("Reversed:")
print(reversed_text)Creates a vertical mirror image of the ASCII art by flipping lines upside down and translating characters to their flipped equivalents.
def flip(self) -> FigletString:
"""
Create vertical mirror image of ASCII art.
Returns:
FigletString: Vertically flipped ASCII art
Behavior:
- Reverses line order (top becomes bottom)
- Translates characters using flip mapping (^->v, etc.)
- Preserves character positions within lines
"""import pyfiglet
# Create ASCII art
text = pyfiglet.figlet_format("Hello", font="standard")
print("Original:")
print(text)
# Flip the text
flipped_text = text.flip()
print("Flipped:")
print(flipped_text)Removes surrounding empty lines while preserving internal structure and leading whitespace within the ASCII art.
def strip_surrounding_newlines(self) -> str:
"""
Remove empty leading and trailing lines.
Returns:
str: ASCII art with surrounding empty lines removed
Behavior:
- Removes empty lines at start and end
- Preserves internal empty lines within the art
- Preserves leading whitespace on each line
- Returns regular string (not FigletString)
"""import pyfiglet
# Create ASCII art (often has surrounding newlines)
text = pyfiglet.figlet_format("Hi", font="standard")
print(f"Original length: {len(text.splitlines())} lines")
print(repr(text))
# Strip surrounding newlines
stripped = text.strip_surrounding_newlines()
print(f"Stripped length: {len(stripped.splitlines())} lines")
print(repr(stripped))Ensures ASCII art has exactly one empty line before and after the content.
def normalize_surrounding_newlines(self) -> str:
"""
Ensure exactly one empty line before and after content.
Returns:
str: ASCII art with normalized surrounding whitespace
Behavior:
- Adds one newline at start
- Adds one newline at end
- Removes any existing surrounding empty lines first
"""import pyfiglet
# Create ASCII art
text = pyfiglet.figlet_format("Test", font="standard")
# Normalize surrounding newlines
normalized = text.normalize_surrounding_newlines()
print("Normalized output:")
print(repr(normalized))Creates new FigletString instances from lists of strings representing lines.
def newFromList(self, list: list[str]) -> FigletString:
"""
Create new FigletString from list of lines.
Parameters:
- list (list[str]): List of strings representing lines
Returns:
FigletString: New FigletString instance with manipulation methods
Behavior:
- Joins lines with newlines
- Adds trailing newline to match FIGlet format
"""import pyfiglet
# Get original FigletString
text = pyfiglet.figlet_format("ABC", font="standard")
# Modify lines manually
lines = text.splitlines()
modified_lines = [line.replace('A', '@') for line in lines]
# Create new FigletString
modified_text = text.newFromList(modified_lines)
print(modified_text)FigletString uses internal translation maps for reverse and flip operations:
/ ↔ \, ( ↔ ), [ ↔ ], { ↔ }, < ↔ >reverse() method^ ↔ v, Some character substitutions for visual flippingflip() methodFigletString methods can be chained for complex transformations:
import pyfiglet
# Create ASCII art
text = pyfiglet.figlet_format("Hello", font="standard")
# Apply multiple transformations
result = text.reverse().flip().strip_surrounding_newlines()
print(result)
# Or step by step
reversed_text = text.reverse()
flipped_text = reversed_text.flip()
final_text = flipped_text.strip_surrounding_newlines()Since FigletString inherits from the standard string type, all regular string methods are available:
import pyfiglet
text = pyfiglet.figlet_format("Hello", font="standard")
# Standard string methods work
lines = text.splitlines()
upper_text = text.upper()
replaced_text = text.replace('H', '@')
# Combined with FigletString methods
modified = text.replace('#', '*').reverse()reverse() and flip() return FigletString instancesstrip_surrounding_newlines() and normalize_surrounding_newlines() return regular strnewFromList() returns FigletString instanceThis design allows for method chaining while providing clean string output when needed.
Install with Tessl CLI
npx tessl i tessl/pypi-pyfiglet