Here's a modified version of the script that accepts command-line arguments to print specific character sets:
import sys
def print_chinese_characters(sample_size=100):
"""Print Chinese (CJK Unified Ideographs) characters."""
print(f"\n--- Printing Chinese (CJK Unified Ideographs) Characters ---")
print(f"Displaying {sample_size} characters from the main CJK block (U+4E00-U+9FFF)")
chinese_chars = []
start_cjk = 0x4E00 # Beginning of common CJK block
for i in range(start_cjk, start_cjk + sample_size):
try:
chinese_chars.append(chr(i))
except ValueError:
continue
if len(chinese_chars) % 50 == 0: # Print in chunks of 50
print("".join(chinese_chars))
chinese_chars = []
if chinese_chars: # Print any remaining characters
print("".join(chinese_chars))
print(f"Total Chinese characters displayed: {sample_size}")
def print_russian_characters(sample_size=256):
"""Print Russian (Cyrillic) characters."""
print(f"\n--- Printing Russian (Cyrillic) Characters ---")
print(f"Displaying characters from the main Cyrillic block (U+0400-U+04FF)")
russian_chars = []
start_cyrillic = 0x0400
end_cyrillic = 0x04FF
for i in range(start_cyrillic, end_cyrillic + 1):
try:
russian_chars.append(chr(i))
except ValueError:
continue
if len(russian_chars) % 50 == 0: # Print in chunks of 50
print("".join(russian_chars))
russian_chars = []
if russian_chars: # Print any remaining characters
print("".join(russian_chars))
print(f"Total Russian characters displayed: {end_cyrillic - start_cyrillic + 1}")
def print_korean_characters(sample_size=100):
"""Print Korean (Hangul Syllables) characters."""
print(f"\n--- Printing Korean (Hangul Syllables) Characters ---")
print(f"Displaying {sample_size} characters from the Hangul Syllables block (U+AC00-U+D7A3)")
print("Note: The full Hangul block has 11,172 characters.")
korean_chars = []
start_hangul = 0xAC00 # Beginning of Hangul Syllables block
for i in range(start_hangul, start_hangul + sample_size):
try:
korean_chars.append(chr(i))
except ValueError:
continue
if len(korean_chars) % 50 == 0: # Print in chunks of 50
print("".join(korean_chars))
korean_chars = []
if korean_chars: # Print any remaining characters
print("".join(korean_chars))
print(f"Total Korean characters displayed: {sample_size}")
def show_usage():
"""Display usage information."""
print("Usage: python script.py <language>")
print("Available languages:")
print(" chinese - Print Chinese (CJK) characters")
print(" russian - Print Russian (Cyrillic) characters")
print(" korean - Print Korean (Hangul) characters")
print("\nExample: python script.py chinese")
def main():
# Check if correct number of arguments provided
if len(sys.argv) != 2:
print("Error: Please provide exactly one argument.")
show_usage()
sys.exit(1)
language = sys.argv[1].lower() # Convert to lowercase for case-insensitive comparison
# Print character encoding information
print("--- Character Encoding Information ---")
char_examples = {
'A': 'ASCII (1 byte in UTF-8)',
'П': 'Cyrillic (2 bytes in UTF-8)',
'中': 'CJK (3 bytes in UTF-8)',
'😀': 'Emoji (4 bytes in UTF-8)'
}
for char, description in char_examples.items():
utf8_bytes = char.encode('utf-8')
print(f"'{char}' (U+{ord(char):04X}): {utf8_bytes} -> {len(utf8_bytes)} byte(s) - {description}")
print("-" * 60)
# Process the language argument
if language == "chinese":
print_chinese_characters()
elif language == "russian":
print_russian_characters()
elif language == "korean":
print_korean_characters()
else:
print(f"Error: Unknown language '{sys.argv[1]}'")
show_usage()
sys.exit(1)
if __name__ == "__main__":
main()
How to use this script:
- Save it as
unicode_printer.py (or any name you prefer)
- Run it from the command line with one of these commands:
python unicode_printer.py chinese
python unicode_printer.py russian
python unicode_printer.py korean
Key features of this updated script:
- Command-line argument parsing: Uses
sys.argv to get the language argument
- Case-insensitive: Converts the input to lowercase, so "Chinese", "CHINESE", or "chinese" all work
- Error handling: Shows usage information if wrong number of arguments or invalid language
- Modular functions: Each language has its own function for better organization
- Consistent output: Each function shows encoding information and character samples
Example usage:
# Print Chinese characters
python unicode_printer.py chinese
# Print Russian characters
python unicode_printer.py russian
# Print Korean characters
python unicode_printer.py korean
# Show error message for invalid input
python unicode_printer.py spanish
The script will display the UTF-8 byte information at the beginning, then print the requested character set. Remember that your terminal needs to support UTF-8 encoding and have appropriate fonts installed to display these characters correctly.