## DEV Community is a community of 722,735 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. Aatmaj

Posted on

# Learning Python-Basic course: Day 8, Unicode in Python

## 🤟 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)
print(ord(a))
``````

Here is a output-

``````Please enter any character Z
90
101
112
104
121
114
49
50
35
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):
for i in range(n1,n2):
print(i," ",chr(i))
``````

Now let us input some values. OUTPUT-

``````Please enter lower limit 33
33   !
34   "
35   #
36   \$
57   9
58   :
59   ;
60   <
61   =
85   U
86   V
87   W
88   X
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
`````` 