Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python chr() and ord()

  • Vijaykrishna RamVijaykrishna Ram
  • January 19, 2020
  • Built-in Methods
Python chr() and ord()

Python’s built-in function chr() is used for converting an Integer to a Character, while the function ord() is used to do the reverse, i.e, convert a Character to an Integer.

Let’s take a quick look at both these functions and understand how they can be used.


The chr() function

Syntax

This takes in an integer i and converts it to a character c, so it returns a character string.

Format:

c = chr(i)

Here is an example to demonstrate the same:

# Convert integer 65 to ASCII Character ('A')
y = chr(65)
print(type(y), y)

# Print A-Z
for i in range(65, 65+25):
    print(chr(i), end = " , ")

Output

<class 'str'> A
A , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , R , S , T , U , V , W , X , Y , Z 

The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in Hexadecimal). ValueError will be raised if the integer i is outside that range.

Let’s verify that with some examples

print(chr(-1))

This will raise a ValueError.

ValueError: chr() arg not in range(0x110000)
start = 0
end = 1114111

try:
    for i in range(start, end+2):
        a = chr(i)
except ValueError:
    print("ValueError for i =", i)

Output

ValueError for i = 1114112

The ord() function

The ord() function takes a string argument of a single Unicode character and returns its integer Unicode code point value. It does the reverse of chr().

Syntax

This takes a single Unicode character (string of length 1) and returns an integer, so the format is:

i = ord(c)

To verify that it does the reverse of chr(), let us test the function using some examples.

# Convert ASCII Unicode Character 'A' to 65
y = ord('A')
print(type(y), y)

alphabet_list = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# Print 65-90
for i in alphabet_list:
    print(ord(i), end = " , ")

Output

<class 'int'> 65
65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 

This raises a TypeError if the length of the input string is not equal to one.

y = ord('Hi')

Output

TypeError: ord() expected a character, but string of length 2 found

Passing Hexadecimal Data

We can also pass Integers represented in other common bases, such as Hexadecimal format (base 16) to chr() and ord().

In Python, we can use Hexadecimal by prefixing an integer with 0x, provided it is within the 32/64 bit range for integer values.

>>> print(hex(18))
'0x12'
>>> print(chr(0x12))
'\x12'
>>> print(ord('\x12'))
18
>>> print(int('\x12'))
18

We pass the integer 18 in hexadecimal format to chr(), which returns a hexadecimal 0x12. We pass that to chr() and use ord() to get back our integer.

Note that we could also get the integer using int(), since a single character string is also a string, which can be a valid parameter to the above function.


Conclusion

In this article, we learned about using chr() and ord() to convert Integers to Characters and vice-versa.


References

  • Python Documentation for chr()
  • Python Documentation for ord()
  • JournalDev article

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Python bytes()
Previous Post Python bytes()
Next Post Encapsulation In Python
Encapsulation In Python

Related Posts

Print Function Python: Arguments and Output

Print Function Python: Arguments and Output

  • November 28, 2026
How to Use Input Function in Python

How to Use Input Function in Python

  • November 28, 2026
How to Open Files in Python

How to Open Files in Python

  • November 23, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.