Wednesday, December 25, 2024
Google search engine
HomeLanguageschr() in Python

chr() in Python

In Python, the chr() function is a built-in function that returns a string representing a character whose Unicode code point is the integer specified. It takes an integer argument and returns the corresponding Unicode character.

Example:

Python3




num = 97
print("ASCII Value of 97 is: ", chr(num))


Output:

ASCII Value of 97 is:  a

Python chr() Function Syntax

Syntax: chr(num)

  • num: an Unicode code integer

Return: Returns str

chr() Function in Python Example

Here, we are going to use Python chr() methods to get the string of Unicode.

chr() in Python

In this example, we are printing Geeks for Geeks with the chr() in Python.

Python3




# Python program to illustrate
# chr() builtin function
 
print(chr(71), chr(101),
chr(101), chr(107),
chr(115), chr(32),
chr(102), chr(111),
chr(114),chr(32),
chr(71), chr(101),
chr(101), chr(107),
chr(115))


Output:

G e e k s   f o r   G e e k s

Get the ASCII Value of Integers in Python

In this example, we are Printing characters for each Unicode integer in the numbers list.

Python3




# Python program to illustrate
# chr() builtin function
 
numbers = [17, 38, 79]
 
for number in numbers:
     
    # Convert ASCII-based number to character.
    letter = chr(number)
    print("Character of ASCII value", number, "is ", letter)


Output :

Character of ASCII value 17 is  ◄
Character of ASCII value 38 is  &
Character of ASCII value 79 is  O

Python chr() to Print Currency Symbol

In this example, we are printing Euro with the chr() in Python.

Python3




unicode_value = 8364
character = chr(unicode_value)
print(character)


Output :

Python Program to Print Emojis

In this example, we are printing smiley with the chr() in Python.

Python3




unicode_value = 128516
character = chr(unicode_value)
print(character) 


Output :

????

Print ASCII Values From 0 to 255 In Python

In this example, we are generating a sequence of integers from 0 to 255.

Python3




unicode_values = range(256)
characters = [chr(value) for value in unicode_values]
print(characters)


Output :

[‘\x00’, ‘\x01’, ‘\x02’, ‘\x03’, ‘\x04’, ‘\x05’, ‘\x06’, ‘\x07’, ‘\x08’, ‘\t’, ‘\n’, ‘\x0b’, ‘\x0c’, ‘\r’, ‘\x0e’, ‘\x0f’, ‘\x10’, ‘\x11’, ‘\x12’, ‘\x13’, ‘\x14’, ‘\x15’, ‘\x16’, ‘\x17’, ‘\x18’, ‘\x19’, ‘\x1a’, ‘\x1b’, ‘\x1c’, ‘\x1d’, ‘\x1e’, ‘\x1f’, ‘ ‘, ‘!’, ‘”‘, ‘#’, ‘$’, ‘%’, ‘&’, “‘”, ‘(‘, ‘)’, ‘*’, ‘+’, ‘,’, ‘-‘, ‘.’, ‘/’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘:’, ‘;’, ‘<‘, ‘=’, ‘>’, ‘?’, ‘@’, ‘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’, ‘[‘, ‘\\’, ‘]’, ‘^’, ‘_’, ‘`’, ‘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’, ‘{‘, ‘|’, ‘}’, ‘~’, ‘\x7f’, ‘\x80’, ‘\x81’, ‘\x82’, ‘\x83’, ‘\x84’, ‘\x85’, ‘\x86’, ‘\x87’, ‘\x88’, ‘\x89’, ‘\x8a’, ‘\x8b’, ‘\x8c’, ‘\x8d’, ‘\x8e’, ‘\x8f’, ‘\x90’, ‘\x91’, ‘\x92’, ‘\x93’, ‘\x94’, ‘\x95’, ‘\x96’, ‘\x97’, ‘\x98’, ‘\x99’, ‘\x9a’, ‘\x9b’, ‘\x9c’, ‘\x9d’, ‘\x9e’, ‘\x9f’, ‘\xa0’, ‘¡’, ‘¢’, ‘£’, ‘¤’, ‘¥’, ‘¦’, ‘§’, ‘¨’, ‘©’, ‘ª’, ‘«’, ‘¬’, ‘\xad’, ‘®’, ‘¯’, ‘°’, ‘±’, ‘²’, ‘³’, ‘´’, ‘µ’, ‘¶’, ‘·’, ‘¸’, ‘¹’, ‘º’, ‘»’, ‘¼’, ‘½’, ‘¾’, ‘¿’, ‘À’, ‘Á’, ‘Â’, ‘Ã’, ‘Ä’, ‘Å’, ‘Æ’, ‘Ç’, ‘È’, ‘É’, ‘Ê’, ‘Ë’, ‘Ì’, ‘Í’, ‘Î’, ‘Ï’, ‘Ð’, ‘Ñ’, ‘Ò’, ‘Ó’, ‘Ô’, ‘Õ’, ‘Ö’, ‘×’, ‘Ø’, ‘Ù’, ‘Ú’, ‘Û’, ‘Ü’, ‘Ý’, ‘Þ’, ‘ß’, ‘à’, ‘á’, ‘â’, ‘ã’, ‘ä’, ‘å’, ‘æ’, ‘ç’, ‘è’, ‘é’, ‘ê’, ‘ë’, ‘ì’, ‘í’, ‘î’, ‘ï’, ‘ð’, ‘ñ’, ‘ò’, ‘ó’, ‘ô’, ‘õ’, ‘ö’, ‘÷’, ‘ø’, ‘ù’, ‘ú’, ‘û’, ‘ü’, ‘ý’, ‘þ’, ‘ÿ’]

What happens if we give something out of range?

Python3




# Python program to illustrate
# chr() builtin function
# if value given is
# out of range
 
# Convert ASCII-based number to character
print(chr(400))


Output :

No Output

We won’t get any output and the compiler will throw an error:

Traceback (most recent call last):
  File "/home/484c76fb455a624cc137946a244a9aa5.py", line 1, in 
    print(chr(400))
UnicodeEncodeError: 'ascii' codec can't encode character 
'\u0190' in position 0: ordinal not in range(128)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments