ord() and chr() function in Python
ord() and chr() function in Python
chr() function
The
chr() function returns the string representing a character whose Unicode
codepoint is the integer.
Note
that on narrow Unicode builds, the result is a string of length two for i
greater than 65,535 (0xFFFF in hexadecimal).
Syntax:
chr(i)
Parameter:
Name |
Description |
Required/ |
i |
A
character whose Unicode codepoint is the integer i. he valid range for the
argument is from 0 through 1,114,111 (0x10FFFF in base 16). Value Error will
be raised if i is outside that range. |
Required |
Return value:
Return the string
representing a character whose Unicode codepoint is the integer i.
Example: Python chr()
function
print(chr(65))
print(chr(97))
print(chr(124))
Copy
Output:
A
a
|
Example: Integer
passed to chr() is out of the range
print(chr(-5))
Copy
Output:
Traceback (most recent call last):
File "/tmp/sessions/0feab4034af997d8/main.py",
line 1, in
print(chr(-5))
ValueError: chr() arg not in range(0x110000)
ord() Function
Definition and Usage
The ord()
function returns the number representing the unicode code of
a specified character.
Syntax
ord(character)
Parameter Values
Parameter |
Description |
character |
String,
any character |
Example
Return
the integer that represents the character "h":
x = ord("h")
Post a Comment