VOOZH about

URL: https://www.geeksforgeeks.org/python/chr-in-python/

⇱ chr() Function in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

chr() Function in Python

Last Updated : 29 Nov, 2023

chr() function returns a string representing a character whose Unicode code point is the integer specified.

chr() Example:


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


Output
E

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.


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.


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.


Output

4. Python Program to Print Emojis

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


Output
????

5. Print ASCII Values From 0 to 255 In Python

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


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


Comment
Article Tags: