Learning Python-Basic course: Day 8, Unicode in Python
Aatmaj
Posted on July 1, 2021
π€ Welcome! Till now, we have dealt enough with numbers. Now is the time to add Unicode to our arsenal.π
Unicode in Python
Python and Java support Unicode characters.π The ord()
method converts a character into its Unicode code. It takes one argument: a string containing a single Unicode character. In other words, given string of length 1, the function returns an integer giving it's corresponding Unicode value. For example, ord('a')
returns the integer 97, ord('β¬')
(Euro sign) returns 8364.
Here is a sample which takes 10 characters and prints their Unicode values-
for i in range(-5,5):#same as (0,10)
a=input("Please enter any character ")
print(ord(a))
Here is a output-
Please enter any character Z
90
Please enter any character e
101
Please enter any character p
112
Please enter any character h
104
Please enter any character y
121
Please enter any character r
114
Please enter any character 1
49
Please enter any character 2
50
Please enter any character #
35
Please enter any character $
36
Try it out with your names too!!!
The chr()
function does just the opposite as the ord()
function. It converts integers into Unicode characters. Example if we input 97, the output will be 'a', and chr(β¬)=8364
Here is a sample program print Unicode characters-
for i in range(0,4):
n1=int(input("Please enter lower limit "))
n2=int(input("Please enter upper limit "))
for i in range(n1,n2):
print(i," ",chr(i))
Now let us input some values. OUTPUT-
Please enter lower limit 33
Please enter upper limit 37
33 !
34 "
35 #
36 $
Please enter lower limit 57
Please enter upper limit 62
57 9
58 :
59 ;
60 <
61 =
Please enter lower limit 85
Please enter upper limit 89
85 U
86 V
87 W
88 X
Please enter lower limit 97
Please enter upper limit 103
97 a
98 b
99 c
100 d
101 e
102 f
Here is another sample to prove that ord()
and chr()
are absolutely opposite of each other
Istrue=True
#Istrue is a boolean value with value default True
for i in range(1,1000):
a=chr(i)
if(i!=ord(a)):
Istrue=False
print(Istrue)
True
Exercise 1) If we interchange ord()
and chr()
, will the program still work? Answer: NO
2) Write a program to give the following output-
Please enter any capital letter G
A B C D E F G
A B C D E F G
A B C D E F G
A B C D E F G
A B C D E F G
A B C D E F G
A B C D E F G
3) Modify the above program slightly to give the following output.
Please enter any capital letter G
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
4) Modify the above program to give the following output-
Please enter any capital letter K
A
B A
C B A
D C B A
E D C B A
F E D C B A
G F E D C B A
H G F E D C B A
I H G F E D C B A
J I H G F E D C B A
K J I H G F E D C B A
The programs may look repetitive, but trust me, they provide a good practice for practicing nested for loops.
βοΈSo friends that's all for now. π Hope you all are having fun.π Please let me know in the comment section below π. And don't forget to like the post if you did. π I am open to any suggestions or doubts. π€ Just post in the comments below or gmail me. π
Thank you allπ
Posted on July 1, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 14, 2021