Exercises

Exercise 11:

Unicode Character Extraction in Python

Write a program in Python which receives two positive integers as input from the user, and then prints all the unicode characters in the interval between the two integers and their corresponding hexa codes.


Code:
 
print("\t\t \u24ca\u24c3\u24be\u24b8\u24c4\u24b9\u24ba")
print("\u2aa2 Unicode Characters for a Range of Integers \u2aa1")
print("\t Programmer: Mohammad Rajabpur")
print("\t pythonize.ir \t 01.21.2023")
print("\t \u2709 info@pythonize.ir")
while True:
    print("-----------------------------------------------")
    print("\u2740 Unicode Characters from n to m \u2740")
    print("\u241B To exit the program \u2794 n or m = exit \n")
    try:
        n = input("n = ")
        n = n.lower()
        if n == "exit":
            print("The program is terminated! Come back soon! \u270b")
            break
        m = input("m = ")
        m = m.lower()
        print("")
        if m == "exit":
            print("The program is terminated! Come back soon!")
            break
        if n.isdigit() == False or m.isdigit() == False:
            print("\u26a0 Error: you have to enter positive integers!")
        else:
            n = int(n)
            m = int(m)
            if n > 1114112 or m > 1114112:
                print("\u26a0 Error: the desired range is out of Unicode range!")
                print("n & m have to be in the range from 0 to 1114112.")
            elif m < n:
                print("\u26a0 Error: m has to be greater than n.")
            else:
                print(" Decimal \u2980 Hexa Code \u2980 Character")
                print(" ---------------------------------")
                for i in range(n,m+1):
                    print("   ", i, " \u2794 ", "\\u" + format(i, '04x'), " \u2794 ", chr(i))
    except:
        print("Error!")

Run:
============== RESTART: C:\Unicode Characters.py ==============
		 ⓊⓃⒾⒸⓄⒹⒺ
⪢ Unicode Characters for a Range of Integers ⪡
	 Programmer: Mohammad Rajabpur
	 pythonize.ir 	 01.21.2023
	 ✉ info@pythonize.ir
-----------------------------------------------
❀ Unicode Characters from n to m ❀
␛ To exit the program ➔ n or m = exit 

n = 1776
m = 1785

 Decimal ⦀ Hexa Code ⦀ Character
 ---------------------------------
    1776  ➔  \u06f0  ➔  ۰
    1777  ➔  \u06f1  ➔  ۱
    1778  ➔  \u06f2  ➔  ۲
    1779  ➔  \u06f3  ➔  ۳
    1780  ➔  \u06f4  ➔  ۴
    1781  ➔  \u06f5  ➔  ۵
    1782  ➔  \u06f6  ➔  ۶
    1783  ➔  \u06f7  ➔  ۷
    1784  ➔  \u06f8  ➔  ۸
    1785  ➔  \u06f9  ➔  ۹
-----------------------------------------------
❀ Unicode Characters from n to m ❀
␛ To exit the program ➔ n or m = exit 

n = 9812
m = 9830

 Decimal ⦀ Hexa Code ⦀ Character
 ---------------------------------
    9812  ➔  \u2654  ➔  ♔
    9813  ➔  \u2655  ➔  ♕
    9814  ➔  \u2656  ➔  ♖
    9815  ➔  \u2657  ➔  ♗
    9816  ➔  \u2658  ➔  ♘
    9817  ➔  \u2659  ➔  ♙
    9818  ➔  \u265a  ➔  ♚
    9819  ➔  \u265b  ➔  ♛
    9820  ➔  \u265c  ➔  ♜
    9821  ➔  \u265d  ➔  ♝
    9822  ➔  \u265e  ➔  ♞
    9823  ➔  \u265f  ➔  ♟
    9824  ➔  \u2660  ➔  ♠
    9825  ➔  \u2661  ➔  ♡
    9826  ➔  \u2662  ➔  ♢
    9827  ➔  \u2663  ➔  ♣
    9828  ➔  \u2664  ➔  ♤
    9829  ➔  \u2665  ➔  ♥
    9830  ➔  \u2666  ➔  ♦
-----------------------------------------------
❀ Unicode Characters from n to m ❀
␛ To exit the program ➔ n or m = exit 

n = 9200
m = 9203

 Decimal ⦀ Hexa Code ⦀ Character
 ---------------------------------
    9200  ➔  \u23f0  ➔  ⏰
    9201  ➔  \u23f1  ➔  ⏱
    9202  ➔  \u23f2  ➔  ⏲
    9203  ➔  \u23f3  ➔  ⏳
-----------------------------------------------
❀ Unicode Characters from n to m ❀
␛ To exit the program ➔ n or m = exit 

n = exit
The program is terminated! Come back soon! ✋