chr() function returns a string representing a character whose Unicode code point is the integer specified.
chr() Example:
Python3
num = 97
print ( "ASCII Value of 97 is: " , chr (num))
|
Output
ASCII Value of 97 is: a
Python chr() Function Syntax
chr(num)
Parameters
- num: an Unicode code integer
Return: Returns str
How to Use Python chr() Function
chr() function in Python is very easy to use, chr() returns string with a Unicode code equal to the integer specified. Let’s look at example for better understanding.
Example
More Python chr() Function Examples
Here, we are going to use Python chr() methods to get the string of Unicode.
1. chr() in Python
In this example, we are printing Geeks for Geeks with the chr() in Python.
Python3
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
2. Get the ASCII Value of Integers in Python
In this example, we are Printing characters for each Unicode integer in the numbers list.
Python3
numbers = [ 17 , 38 , 79 ]
for number in numbers:
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
3. 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)
|
4. 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)
|
5. 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...
6. What happens if we give something out of range?
Output :
No Output
We won’t get any output and the compiler will throw an error:
Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 7, in <module>
print(chr(-400))
ValueError: chr() arg not in range(0x110000)
In this article we discussed the working, uses and examples of Python chr() function. chr() function is very easy and fun to use. It is very useful when working with Unicode encoded data.
Hope you learnt about Python chr() with different examples
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Nov, 2023
Like Article
Save Article